यहाँ AJAX के साथ एक विकल्प है, लेकिन कोई jQuery नहीं, बस नियमित जावास्क्रिप्ट:
इसे पहले / मुख्य php पृष्ठ पर जोड़ें, जहाँ से आप कार्रवाई को कॉल करना चाहते हैं, लेकिन इसे एक संभावित aटैग (हाइपरलिंक) से बदलकर एक buttonतत्व कर सकते हैं, इसलिए यह किसी भी बॉट या दुर्भावनापूर्ण एप्लिकेशन (या जो भी) से क्लिक नहीं होता है।
<head>
<script>
// function invoking ajax with pure javascript, no jquery required.
function myFunction(value_myfunction) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("results").innerHTML += this.responseText;
// note '+=', adds result to the existing paragraph, remove the '+' to replace.
}
};
xmlhttp.open("GET", "ajax-php-page.php?sendValue=" + value_myfunction, true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php $sendingValue = "thevalue"; // value to send to ajax php page. ?>
<!-- using button instead of hyperlink (a) -->
<button type="button" onclick="value_myfunction('<?php echo $sendingValue; ?>');">Click to send value</button>
<h4>Responses from ajax-php-page.php:</h4>
<p id="results"></p> <!-- the ajax javascript enters returned GET values here -->
</body>
जब buttonक्लिक किया जाता है, तो इस से पहले कई उदाहरणों की तरह, onclickहेडेक्स जावास्क्रिप्ट फ़ंक्शन का उपयोग $sendingValueअजाक्स के माध्यम से दूसरे php-पेज पर भेजने के लिए किया जाता है। अन्य पेज, ajax-php-page.phpGET मान के लिए जाँच करता है और साथ लौटता है print_r:
<?php
$incoming = $_GET['sendValue'];
if( isset( $incoming ) ) {
print_r("ajax-php-page.php recieved this: " . "$incoming" . "<br>");
} else {
print_r("The request didn´t pass correctly through the GET...");
}
?>
print_rतब से प्रतिक्रिया लौटा दी जाती है और साथ प्रदर्शित की जाती है
document.getElementById("results").innerHTML += this.responseText;
+=भरता है और, मौजूदा HTML तत्वों में कहते हैं को हटाने +सिर्फ अद्यतन और एचटीएमएल की मौजूदा सामग्री की जगह pतत्व "results"।