2

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.

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";
}
Mark Augias
  • 271
  • 1
  • 7
  • Never apologize for an abundance of information on your question. The more info we have the easier it is to spot the issue and give you an answer. – SuperMykEl Jun 19 '12 at 16:11
  • Cheers my man, appreciate it... See edits above @SuperMykEl – Mark Augias Jun 20 '12 at 10:31
  • Make sure you add the id attr to you hidden input – SuperMykEl Jun 20 '12 at 18:18
  • Sorry, the id was there in my working code, forgot to update it here. Still no joy... Referencing this article: http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page?lq=1 He reckons you should have $var_value = $_SESSION['varname']; on the landing page, i've tried with that but no joy either. This is totally odd? Unless for some reason WordPress is blocking the script writing to usersession.php? Can anyone think of a way of how I can test to see if usersession.php is receiving the variable? @SuperMykEl – Mark Augias Jun 20 '12 at 19:41
  • try changing the following line if (data == "1") to if (data === "1") – SuperMykEl Jun 20 '12 at 19:55
  • No joy I'm afraid... I put in one js alert for the if and another for the else statement of that script and it's the else that popped up... which tells me there's something wrong with that first part of the script, I guess it's not finding the input value... I just can't see why? mental. – Mark Augias Jun 20 '12 at 20:11
  • Try an alert to echo $("#conv").val() – SuperMykEl Jun 20 '12 at 20:22
  • Nice one, put the alert in the else part and it popped up with the desired value. Sweet! So, that if condition isn't being met, data isn't equalling 1... but we do have the desired value – Mark Augias Jun 20 '12 at 20:36
  • Check my answer for the update. I think i found it. – SuperMykEl Jun 20 '12 at 22:33

1 Answers1

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());
        }
);
SuperMykEl
  • 613
  • 3
  • 15
  • 1
    either that, or change jquery selector to `$('input[name=conv]')` – poncha Jun 20 '12 at 19:29
  • Thanks mate, a welcome spot, but unfortunately hasn't fixed it... this is such an odd one?! I've updated the javascript, I'm getting the alert with the desired value on form submit... but still not getting the value to appear on the landing page. I'm pretty sure the process is half way there, as in we're getting the data from the form and submitting it to usersession.php... but just not getting it from usersession.php to the landing page. Does the usersession and landing page code look ok do you think? – Mark Augias Jun 21 '12 at 09:05
  • Cheers @SuperMykEl... Sorry to of only just got back to you. Still no glory :-( I've updated the original question with changes I've made, but still no joy. There's some more insight to my thinking just under the -EDIT - section of my post if you've got time to cast an eye? Thanks so much... – Mark Augias Jun 26 '12 at 14:42
  • NP @MarkAugias, first thing I would do is to alert the data now that we have verified that you have the right value in the `$('#conv')`, so change that line to `alert (data)`, and see what is happening there. Also if you are not already using it, get firebug, and learn to use the console real quick (it is super easy, just read a little of the docs, and you are set there). You can check whether the post call is being made, and to the correct file, and see the headers(to make sure your post is posting the correctly), as well as seeing what the usersession.php is returning. What browser do U use? – SuperMykEl Jun 26 '12 at 15:14
  • Sorry mate, replace... alert("Data Loaded: " + $("#conv").val()); with alert(data);? I use IE6. Ha. No I actually just moved from FF to Chrome, but I can still fire up FF with Firebug. Will have a peek and report back tomorrow. Big thanks mate! @SuperMykEl – Mark Augias Jun 26 '12 at 20:01
  • hit `F12` or `ctrl+shift+I` in Chrome. Has basically the same thing – SuperMykEl Jun 26 '12 at 21:02
  • Ok, so I had a play with the FireBug... I had to quickly abort the submission before the redirect took place as I'd loose any data it was telling me, of which was this: http://cl.ly/31371a3G0D3M1d201I01 90 is the correct value, so that tells me the value has been set and is being sent, right? I've made some more changes to the js code in original post, so please review that... feedback, as ever, welcome. @SuperMykEl – Mark Augias Jun 27 '12 at 16:06
  • Read from: I figured I don't actually... For where I'm at :-) – Mark Augias Jun 27 '12 at 16:18