Krijn van der Burg Krijn van der Burg - 10 months ago 47
Ajax Question

Jquery var to php but do not return

I have a HTML form that I want to send to PHP but also some javascript variables. These JS vars are not in the form. How can I get both to th PHP class?

Below is the code I have so far.

$("#login_FORM").submit(function() {
//.. serialized form data
window.location.href = "";

variable ends up perfectly in
but when it gets to the end of the php file... it returns to above javascript function.

Even when using a
into php it returns to the javascript function. It simply reads that page as well and returns everything thats returned in there.
echo $_POST['hash'];
header('Location: ../index.php'); die();

Only if I use a
window.location.href = "";
in javascript I can redirect to a correct page after processing the javascript data in the
file (redirecting to correct page isn't the only reason I'm asking the

How can I send variables from jquery / javascript to PHP and continue from there? aka not return to the (success) function the variables were send from.


If I understand you correctly you want to pass the serialized form data plus some jquery variables if that's the case you could use serializeArray()

var data = $('#myForm').serializeArray();
data.push({var1: 'value', var2: 'value'});

$.post("myPage.php", data);


I think i got what you are trying to do, if you submit the form using jquery ajax it will always return to the success function because any redirection in php will only redirect the ajax request and not the current "screen".In that case try the following (not a 100% but here it goes): add hidden fields to your form say:

<form action="/class/login.Class.php">
    <!-- form content n stuff -->
    <input id="var1" name="var1" type="hidden" value="someValue" />
    <button id="letsGo" type="submit">Submit</button>

$('#letsGo').click(function(e) {
    var form = $(this).closest('form');
    // replace as many hidden inputs as you need

Hope this helps.