Hi,
Using JavaScript you can communicate between PHP and JavaScript using AJAX calls. This is a very basic example using the jQuery library as it can simplify the use of AJAX calls.
Code:
$.post(
"./some_page.php"
, {first_name: $("#first_name").val();
, last_name: last_val
}
, function(data){
/* If data comes back from the PHP page this is where you get it.
You can return any type of data. More complex data you
may consider converting arrays to JSON,
but this example using simple text name values */
var name = firs_name + ' ' + last_name;
if(data == name){
var error = name+ ' ' + 'already exists. It was not added.';
alert(error);
$("#table_data").html('<p>' + error +'</p>');
return 0;
}
/* If no errors then finish with these JavaScript functions*/
displayMessage(data);
setTimeout(function(){hideMessage();}, 4000);
search(add_set);
});
Now in your PHP page that gets called by AJAX, you handle the $_POST values like you would any other time. However you will notice that I don't (in this example) return values instead I echo the finished presentation that the JavaScript displays.
PHP Code:
<?php
/* some_page.php */
if($_POST['first_name'] && $_POST['last_name']){
// Do your php work like SELECT, INSERT, or UPDATE you database with the post values that came from the AJAX call
...
...
if($success){
echo $name;
} else {
echo 0;
}
}
Bookmarks