jquery ajax call php function with parameters

jquery ajax call php function with parameters

Welcome to the In Pakainfo.com website! You will Step By Step learn web programming, easy and very fun. This website allmost provides you with a complete web programming tutorial presented in an easy-to-follow manner. Each web programming tutorial has all the practical examples with web programming script and screenshots available.For

jquery ajax call php function with parameters

AJAX script is used to very effective create more interactive web-applications.

$.ajax({
	url: 'php/new-user.php',
	type: 'POST',
	dataType: "json",
	data: {
		name: $('#username').val(),
		email: $('#useremail').val(),
		password: $('#userpassword').val()
	}
}).done(function(result){
		alert(JSON.stringify(result));
});

---------

<input type="text" id="username" class="form-control">
<input type="text" id="useremail" class="form-control">
<input type="password" id="userpassword" class="form-control">

how to call php function from javascript using ajax example

This is a basic JQuery Ajax GET request:

$.ajax({
    type: "GET",
    url: 'getdata.php',
    success: function(responseText){
        alert(responseText);
    }
});

Execute php file with Ajax

query string parameters : Calling PHP file using AJAX

$.ajax({
    type: "GET",
    url: 'web.php',
    data: {webname: 'Wayne'},
    success: function(responseText){
        alert(responseText);
    }
});

call php function on button click using ajax

add multiple GET variables – query string parameters

$.ajax({
    type: "GET",
    url: 'web.php',
    data: {webname: 'Wayne', webage: 27, webcountry: 'Ireland'},
    success: function(responseText){
        alert(responseText);
    }
});

Javascript (jQuery):

<script>
   $.ajax({

     url : 'action/getdata.php',
     type : 'POST',
     success : function (responseText) {
        console.log (responseText); // Here, you need  some to use data response by PHP file example.
     },
     error : function () {
        console.log ('error');
     }

   });
</script>

Content of getdata.php is:

<?php
    echo "pakainfo is angularjs tutorials";
?>

AJAX can be used very easy for very interactive to communication with a mysql database.

index.html

 <html>
<head>
<script>
function showweb(str) {
    if (str == "") {
        document.getElementById("suggest_txt").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // here is code for IE7+ browser, Firefox browser, Chrome browser, Opera browser, Safari browser
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5 browser
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("suggest_txt").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
<select name="webslits" onchange="showweb(this.value)">
  <option value="">Select a website:</option>
  <option value="1">pakainfo</option>
  <option value="2">w3free</option>
  <option value="3">ng4free</option>
  <option value="4">angularjs</option>
  </select>
</form>
<br>
<div id="suggest_txt"><b>website info will be all the info listed here...</b></div>

</body>
</html>

PHP File (getuser.php)

<!DOCTYPE html>
<html>
<head>
<title>PHP - AJAX and PHP Example</title>
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>webName</th>
<th>webInfo</th>
<th>validity</th>
<th>webdomain</th>
<th>webpoint</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['webName'] . "</td>";
    echo "<td>" . $row['webInfo'] . "</td>";
    echo "<td>" . $row['validity'] . "</td>";
    echo "<td>" . $row['webdomain'] . "</td>";
    echo "<td>" . $row['webpoint'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

Example

Simple Ajax request example with JQuery and PHP | jQuery Ajax Call to PHP Script with JSON Return | how to call php function from javascript using ajax | jquery ajax call php function with parameters


Related FAQ

Here are some more FAQ related to this Article:

  1.   Like Dislike system with PHP and MySQL
  2.   Dynemically HTML table from AJAX response using PHP
  3.   jQuery Check an Element is Visible or not
  4.   Handling Arrays using jQuery Tutorial with Examples
  5.   how to disable previous date in bootstrap datepicker?
  6.   how to create forgot password Recovery (Reset) in php and MySQL?
  7.   How to resolve the "javascript runtime error function is undefined"?
  8.   Simple Multiple Countdown timers using jquery
  9.   Send Forgot password by mail or message in PHP
  10.   json.stringify - Convert Object to String