使用AJAX将PHP变为javascript? [英] Php variable to javascript with AJAX?

查看:77
本文介绍了使用AJAX将PHP变为javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy
如何从PHP获取变量值,我修改了这段代码:

JavaScript Ajax发布到PHP文件XMLHttpRequest对象返回数据教程 [

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[

Ajax Post to PHP File XMLHttpRequest Object Return Data Tutorial



]



如果我用PHP计算某些东西.. 。并在PHP中创建数组,如何在这个JS函数中传递值/ ajax_post()/示例中的内容?



我有什么试过:




]

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:

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





example.html



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>

推荐答案

_POST [' firstname']。 ' '
_POST['firstname'] . ' ' .


_POST [' lastname']。 ' ,表示PHP文件';
?>
_POST['lastname'] . ', says the PHP file'; ?>





example.html



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>


Quote:

how我可以从PHP中获取变量值,我修改了这段代码:

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



你可以这样做


You can do like

var data = <?php echo json_encode("42", JSON_HEX_TAG); ?>;



从您的代码中很难理解,您在何处尝试访问该值从您的PHP代码。



还有其他几种方法可以做到这一点。我强烈建议您在继续对代码进行更改之前阅读StackOverflow中的以下主题。

如何将变量和数据从PHP传递给JavaScript? - Stack Overflow [ ^ ]



如果你的要求不是这个,请告诉我。

希望,它有帮助:)


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 :)


这篇关于使用AJAX将PHP变为javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆