How to use Ajax using jQuery in PHP?
Firstly, you have to include the
<script src=”https://code.jquery.com/jquery-1.12.0.min.js”></script> before </head> Or use the latest jQuery version as required.
Then, use the main content of the page as below code:
<ul>
<li>
Each year World Red Cross and Red Crescent Day is celebrated on</li>
<input type=’radio’ name=ans class=’er’ value=’1′>June 8<br>
<input type=’radio’ name=ans class=’er’ value=’2′>May 8
</li>
</ul>
<span class=’button’ id=’btnsbmt’>NEXT</span>
<div id=”resultarea” style=”color:#ff0000″></div>
<script type=”text/javascript”>
jQuery(‘#btnsbmt’).click(function(){
var getValue = jQuery(‘input[name=ans]:checked’).val();
var request = jQuery.ajax({
url: “ajax.php”,
data: {userAnswer : getValue},
dataType: “html”,
success: function(data){
jQuery(“#resultarea”).text(data);
}
});
});
</script>
</div>
After that, you have to create a new file ajax.php and place it on the same server. Add the below code in that and enjoy:
echo $userAnswer = $_REQUEST[‘userAnswer’]; //get value from the ajax to validate with the database/**
Compare the $userAnswer value with the database and do the required thing.
You can return the required values from here.
*/
?>
This is the demo example. You can exapnd using the above example and make the new pages using ajax.
Leave a comment