Sending PHP variable, via jQuery to landing page whilst skipping third party submission script?

My goal is to set a Google Conversion value from a custom field defined in WordPress. The conversion script is located on the landing page, so I need to get my custom field data from my form to the landing page. I can’t use GET or POST as the form submission is handled by a third party and no data is returned to the actual landing page.

So I’ve tried using a PHP session, but this third party is getting in the way of just being able to use PHP, because it’s keeping all the data for itself.

Read More

This is the approach I’m hoping I can get working:

  • The validation for the form is done using jQuery Tools.
  • I then need to submit the variable after validation has been successful via jQuery/AJAX to a separate php file.
  • Then as the landing page starts to load, I must grab that variable from mentioned PHP file and echo it in the relevant place.

I figured I don’t actually need to start a session on the page with the form, as jquery is grabbing the data straight out the input, not any session data. So here’s my input with conversion value:

<input type="hidden" id="conv" name="conv" value="90">

Then my form validation:

    $("#course-form-modal").validator().submit(function(e)  {
           // when data is valid
           if (!e.isDefaultPrevented()) {

                    // this grabs the value from my form
            var con_val = $("#conv").val();
            // and this sends it...
            $.post(
                "../../usersession.php", 
                { data: con_val } 
            );

           }

    });

Then I’ve got the code in usersession.php… where I sent the data:

// As I'm just trying to echo what was sent to this page, via ajax, I shouldn't need to worry about starting/retrieving a SESSION yet... right?
<?php $var_value = $_POST['data']; ?>
<div id="results">
<?php echo $var_value ?>    
</div>

// I CAN WORRY ABOUT THIS HALF LATER. RIGHT NOW I JUST WANT TO ECHO MY RESULTS ON USERSESSION.PHP //

Finally, I’ve got the code on my landing page to retrieve the data from usersession.php:

session_start();
$var_value =  $_SESSION['conv']; 
echo $var_value;

I’m not entirely sure all this code is right for starters, I’m more of a front end guy…

-EDIT-

Right, I’m pretty sure the code is correct at least now. For some reason it’s still not working though. At the moment I’m wondering if WordPress would prevent me writing to usersessions.php from my javascript file (for reference, that file path is set absolutely in my working (not working) example)? I know WordPress will sometimes throw a 404 when you try to access a file directly.
The other potential issue could be with the third party software, vanillasoft. I’ve a link to their script in the action tag of my form, could that somehow bypass/kill the sending/receiving of data between the form > usersession.php > and then the landing page?

On a side note, if anyone has a great idea on how I can test if usersession.php is receiving the data then please let me know? I did have this code originally, but it returns nothing and if I link straight to the file after a send something (as in just paste the file url in to my browser) it returns a ‘0’…

if(isset($_POST['conv'])) {
    session_start();
    $_SESSION['conv'] = $_POST[''conv''];
    echo "1";
} else {
    echo "0";
}

Related posts

Leave a Reply

1 comment

  1. Set your ID on the input. jQuery is looking for the ID, but you have only set the name.

    <input type="hidden" name="conv" value="90">
    

    Should be:

    <input type="hidden" name="conv" id="conv" value="90">
    


    EDIT:

    Can’t believe I didn’t catch this earlier. Your problem is in the usersession.php at the following line.

    $_SESSION['conv'] = $_POST[''conv''];
    

    You have the POST quoted wrong.
    It should be:

    $_SESSION['conv'] = $_POST['conv'];
    


    EDIT (re: New js edits)

    In you java script your post vars should be formatted thusly:

    { name: "John", time: "2pm" }
    

    So your line should be something like this:

    $.post(
            '../../usersession.php', 
            {
                conv: $("#conv").val()
            }, 
            function(data) 
            {
                alert("Data Loaded: " + $("#conv").val());
            }
    );