Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hy how can I get back variable value from PHP , I modified this code:
JavaScript Ajax Post to PHP File XMLHttpRequest Object Return Data Tutorial[
JavaScript
Ajax Post to PHP File XMLHttpRequest Object Return Data Tutorial

]

If I calculate something in PHP...and create array in PHP , how can I pass the value in this JS function/ajax_post()/ what is in the example?

What I have tried:

PHP
my_parse_file.php
<?php 
echo 'Thank you '. $_POST['firstname'] . ' ' . $_POST['lastname'] . ', says the PHP file';
?>


example.html
<html>
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "my_parse_file.php";
    var fn = document.getElementById("first_name").value;
    var ln = document.getElementById("last_name").value;
    var vars = "firstname="+fn+"&lastname="+ln;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
	    if(hr.readyState == 4 && hr.status == 200) {
		    var return_data = hr.responseText;
			document.getElementById("status").innerHTML = return_data;
	    }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
First Name: <input id="first_name" name="first_name" type="text">  <br><br>
Last Name: <input id="last_name" name="last_name" type="text"> <br><br>
<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
<div id="status"></div>
</body>
</html>
Posted
Updated 7-Sep-19 2:31am
Comments
Where is the issue? Did you debug?

Quote:
how can I get back variable value from PHP , I modified this code:

You can do like
PHP
var data = <?php echo json_encode("42", JSON_HEX_TAG); ?>;

It's hard to understand from your code, where exactly you are trying to access the value from your php code.

There are few other ways available to do this. I would strongly recommend to read following thread in StackOverflow before you go ahead to do the changes in your code.
How to pass variables and data from PHP to JavaScript? - Stack Overflow[^]

Please let me know if your requirement is something else than this.
Hope, it helps :)
 
Share this answer
 
v3
index.html
<html>
<head><title></title></head>
<body>

<button onclick="display_data()">view</button>
<div id="show"></div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js">
</script>
<script type="text/javascript" id="display"></script>

<script>
function display_data(){
var s = 1;
    var dataString = 'show=' + s;
$.ajax({

type: "POST",
url: "blog.php",
data: dataString,
success: function(data){

	document.getElementById("display").innerHTML = data;
	replace();
}

});
    
}
</script>
</body>
</html>

blog.php
<?php
if (isset($_POST['show'])) {

	$date = date("d/M/Y");
   echo '
	
		function replace() {
			var d = "'.$date.'";
			document.getElementById("show").innerHTML = d;
			
		}
	';
}
?>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900