drayarms Posted March 15, 2012 Share Posted March 15, 2012 What I thought was a very simple process turned out to be a nightmare. I'm trying to asynchronously display a message on an HTML page after a value has been entered into a form field on the same page. The form input field in question is as follows: <input id = "input_email" type="input" name ="email" value ="" size="16" maxlength="14"/> The script below is meant to collect the value of the form input and send to a PHP form, which would then display a message in a div with id "email_error" if the input is received. $(document).ready(function() { //Check if email is already in database. $("#input_email").blur(function(){ var data_string = $("#input_email").val; //Send input asynchronously to server $.post("check_email_input.php",{data:data_string}, function(data){ $("#email_error").html(data); }); }); In the PHP script, I do a check to see if the form input has been received and then echo it. I also print out a dummy message below it. <?php //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //Check if data was posted if(isset($_POST['email'])){ $email = $_POST['email']; echo $email; } //Dummy message echo"<span>Nothing is happening</span>"; ?> The dummy message gets printed everytime, but not the data from the form. I have tried other variations like $_POST['data'] instead of $_POST['email'], $("$input_email").serialize(); instead of $("input_email").val, all to no avail. What's to be done? Link to comment https://forums.phpfreaks.com/topic/259012-how-do-i-send-form-input-data-to-a-php-script-via-ajax/ Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 This line: $.post("check_email_input.php",{data:data_string}, means that you are sending a $_POST value to the server with the key 'data'. You are trying to use the key 'email' to grab this data, obviously this will not work. You need to use $_POST['data'] instead. Link to comment https://forums.phpfreaks.com/topic/259012-how-do-i-send-form-input-data-to-a-php-script-via-ajax/#findComment-1327854 Share on other sites More sharing options...
drayarms Posted March 15, 2012 Author Share Posted March 15, 2012 @aykay47, that's what i thought. i used that initially and it wouldn't work. so i got the other idea from looking at some online examples though i didn't think it made sense. Link to comment https://forums.phpfreaks.com/topic/259012-how-do-i-send-form-input-data-to-a-php-script-via-ajax/#findComment-1327871 Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 Well, it needs to be $_POST['data'] I believe it is because you are accessing the val() method as a property in your code, which it's not.. Should be .val(); not .val; Link to comment https://forums.phpfreaks.com/topic/259012-how-do-i-send-form-input-data-to-a-php-script-via-ajax/#findComment-1327897 Share on other sites More sharing options...
drayarms Posted March 16, 2012 Author Share Posted March 16, 2012 @akay47, i now know why i couldn't get $_POST['data'] to work previously. It is because I had val instead of val(). I fixed that and it works now. Thanks for contribution. Link to comment https://forums.phpfreaks.com/topic/259012-how-do-i-send-form-input-data-to-a-php-script-via-ajax/#findComment-1327991 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.