1

I'm trying to post a serialized data from jQuery to php like the following.

$.post(location.href, $('form').serialize(), function(data){
    // do something
});

It's working very well. But is there a way I can set a name to the serialized data, so I can use that name for isset() function? It'll look like this:

Let's assume the form has

<input type="text" name="input1">
<input type="text" name="input2">

file.js

$.post(location.href, {data:$('form').serialize()}, function(data){
    // do something
});

file.php

<?php
    if (isset($_POST['data'])) {
        // retrieve the data in the serialized string and use it in a function
        helperFunction($_POST['input1'], $_POST['input2']);
    }
?>

<html>
    // rest of the layout
</html>

I don't want to use a button to submit the form. The reasons I don't want it are:

  1. Using <button type="submit">Submit</button>, then submit the form to the same page will ask for re-submission when I refresh the page (which I don't want it) after I submit the form.
  2. Using jQuery to post the serialized data to the server will not ask for re-submission when I refresh the page after I post.

2 Answers 2

2

you can add a hidden variable and check it in post value.

<input type="hidden" name="check_val" value="chek_val" />
<?php

if(isset($_POST['check_val'])) {

} ?>

Sign up to request clarification or add additional context in comments.

2 Comments

How does this actually work? That hidden input will be inside the serialized string as well. Or do you mean that hidden input will always be set; thus, the form is being posted to server?
I chose this answer because of its simplicity for debugging and modifying current code. And it works great!
1

Since PHP parse arrays from submitted POST data you can manipulate like this:

<input type="text" name="inputs[data][1]">
<input type="text" name="inputs[data][2]">

<?php
    if( isset( $_POST[ 'inputs' ][ 'data' ] ) ) {
        helperFunction( $_POST[ 'inputs' ][ 'data' ][ '1' ], $_POST[ 'inputs' ][ 'data' ][ '2' ] );
    }
?>

But actually I don't see a reason why not to do more simple:

<input type="text" name="input1">
<input type="text" name="input2">

<?php
    if ( isset( $_POST[ 'input1' ] ) &&
         isset( $_POST[ 'input2' ] ) ){
        helperFunction( $_POST[ 'input1' ], $_POST[ 'ipnut2' ] );
    }
?>

3 Comments

I don't do that because my forms have a lot of inputs, selects, and/or, textareas. Do you have another way for lots of data?
@Lancelot Why don't you try the first way provided? Group data into arrays.
I prefer not to, because the first way will be hard to think (when debuggin/reading the code) which data I'm actually passing to the function. However, I do appreciate your suggestion as I didn't know I can put it as an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.