0

This form is working perfectly fine, e.g. it stops the form submitting, there's no javascript errors in the console log. However, when I press submit, I get an alert with nothing in it. I have searched the internet for multiple solutions and they all give the same for me.

Here's my Javascript/Html

<script>
$(function(){
    $("#submit").click(function(event){ 
        event.preventDefault();
        $.ajax({
            type:"post",
            url:"templates/process-token.php",
            data:$("#form").serialize(),
            success:function(response){
                alert(response);
            }
        });
    });
});
</script>

<form id="form" method = "post" action = "security_check">
    <input type = "text" name = "fld1" id = "fld1" />
    <input type = "text" name = "result1" id = "result1" value = "value_from_php_page" disabled />
    <input type="submit" value="Submit" id="submit">
</form>

And here's the PHP, it's so simple but doesn't work:

<?php
    echo $_POST["fld1"];
?>

Any help here would be appreciated. Thank you.

Jarrod
  • 322
  • 1
  • 3
  • 13

3 Answers3

1

You could do this with JSON both ways. Here's how I would implement this kind of thing in JavaScript and PHP.

There's a great function in jQuery that is a shortcut for getting JSON data. It's a shortcut for $.ajax specifically for simple calls.

$.getJSON( "templates/process-token.php?"+$("#form").serialize(), function( data ) { alert( data.reponse ) } );

PHP could return a json encoded string so it can be accessed from the JavaScript code more easily.

<?php echo json_encode( array('response'=>$_POST['fld1']) ); ?>

adamS
  • 592
  • 8
  • 14
1

Change data from $('#form').serialize() to a one test variable so you can see if it is JS or PHP that is causing the issue.

On your PHP page:

<?php
   isset($_POST["fld1"]) ? $myvar1 = $_POST["fld1"] : $myvar1 = 'No data passed...';
   echo $myvar1;
?>

This confirms that there is infact an fld1 in the $_POST, and if not, says so, taking away the guess work...

Casey Dwayne
  • 2,142
  • 1
  • 17
  • 32
  • Very good, thank you. I did try this with a previous version of the code and didn't work. However, this time it did. Thanks for your help @kcdwayne, have a good day. – Jarrod Jan 24 '14 at 13:21
  • You're welcome, happy to help. It's always a good idea to setup a method of handling (in this case, preventing) errors both client and server side. Empty/undefined errors can cause a lot of problems, and without having feedback from the program it can really leave you guessing! By the way, for development, you should have your error reporting turned on. Just remember to disable once it's working on the server! http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php – Casey Dwayne Jan 24 '14 at 13:47
  • Yeah, I fixed that up too. This was disabled from previous projects :) Again, thanks for your help. – Jarrod Jan 24 '14 at 14:06
0

That can happen only if the text box #fld1 is empty. Type something in the text box and it will work.

Zvonimir Burić
  • 528
  • 3
  • 11