Although I'm still reading Nicholas C. Zakas' book (read over 300 pages within 4 days), in the meantime I got a little project to create a half-automatized documentation generator page for our later big project. It's half-done, but now I want to give more features to it which requires that I need to send the values of textareas to PHP, so PHP can update the mysql and vica versa, but that's not the matter now.
The problem: Ajax succeed, I get the success message, but PHP doesn't read the POST. Why?
I made my research, I read tones of stackoverflow topics and other websites, phpmanual, w3school examples, but something is going wrong.
Basic html example:
<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
<div id="result"></div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="script.js"></script>
The basic ajax call(send) I found for this:
/* Attach a submit handler to the form */
$("#foo").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
/* Clear result div*/
$("#result").html('');
/* Get some values from elements on the page: */
var values = $("#foo").serialize();
console.log("val: "+values);
/* Send the data using post and put the results in a div */
$.ajax({
url: "ez.php",
type: "POST",
data: values,
success: function(){
$("#result").html('Submitted successfully');
},
error:function(){
alert("failure");
$("#result").html('There is error while submit');
}
});
});
Simple PHP for test if I get that data or not
<?php
if(isset($_POST['bar']))
{
echo "hello " . $_POST['bar'];
};
?>
All three files are in the same folder.
I'm surely will learn about this topic more and deeper later on, just now I need to finish this small project as soon as possible. I'm going to be front-end developer, so the php part won't wait for me, but now I need it for testing until I can speak with the server-side programmer. I want to be sure which variables and data I'll send to him, so he can handle them later.
Thanks in advance!