Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从PHP发送和接收数据_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 从PHP发送和接收数据

Javascript 从PHP发送和接收数据,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我不熟悉web开发,尤其是PHP、jQuery、AJAX 我试图让用户输入一个名称,然后index.html将其发送到一个php文件,php文件将其保存到一个名称文件中,并在网站上更改一个带有状态id的div元素。 我无法找出问题所在,div元素的内部HTML保持“正在处理…” index.html <html> <head> <script> function ajax_post(){ document.getElementById("status"

我不熟悉web开发,尤其是PHP、jQuery、AJAX

我试图让用户输入一个名称,然后index.html将其发送到一个php文件,php文件将其保存到一个名称文件中,并在网站上更改一个带有状态id的div元素。 我无法找出问题所在,div元素的内部HTML保持“正在处理…”

index.html

<html>
<head>
<script>
function ajax_post(){
    document.getElementById("status").innerHTML = "processing...";
    $.post("server.php", { username: $("#userName").val() }, function(data) {
    $("#status").html(data);
    })
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
Username: <input id="userName" name="userName" type="text">  <br><br>
<input name="myBtn" type="submit" value="Check" onclick="ajax_post();"> <br><br>
<div id="status"></div>
</body>
</html>

函数ajax_post(){
document.getElementById(“status”).innerHTML=“处理…”;
$.post(“server.php”,{username:$(“#username”).val()},函数(数据){
$(“#状态”).html(数据);
})
}
Ajax发布到PHP并获取返回数据
用户名:



server.php

<?php 
if(!empty($_POST['username'])){
        $data = $_POST['username'];
        echo 'You are now registered,' . $data;
        $fname = "save.text";

        $file = fopen($fname, 'a');
        fwrite($file, $data . "\n");
        fclose($file);
}
?>


我假设我的$.post语法是错误的,但我并不真正理解它。

试试看,我已经执行了这段代码,它对我很有用

Index.html

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function ajax_post(){
    document.getElementById("status").innerHTML = "processing...";
    console.log($("#userName").val());
    $.post("server.php", { username: $("#userName").val() }, function(data) {
    $("#status").html(data);
    })
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
Username: <input id="userName" name="userName" type="text">  <br><br>
<input name="myBtn" type="submit" value="Check" onclick="ajax_post()"> <br><br>
<div id="status"></div>
</body>
</html>

函数ajax_post(){
document.getElementById(“status”).innerHTML=“处理…”;
log($(“#用户名”).val());
$.post(“server.php”,{username:$(“#username”).val()},函数(数据){
$(“#状态”).html(数据);
})
}
Ajax发布到PHP并获取返回数据
用户名:



Server.php

<?php 
if(!empty($_POST['username'])){
        $data = $_POST['username'];
        echo 'You are now registered,' . $data;
        $fname = "save.text";

        $file = fopen($fname, 'a');
        fwrite($file, $data . "\n");
        fclose($file);
}
?>

试试这个,我已经执行了这个代码,它对我很有效

Index.html

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function ajax_post(){
    document.getElementById("status").innerHTML = "processing...";
    console.log($("#userName").val());
    $.post("server.php", { username: $("#userName").val() }, function(data) {
    $("#status").html(data);
    })
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
Username: <input id="userName" name="userName" type="text">  <br><br>
<input name="myBtn" type="submit" value="Check" onclick="ajax_post()"> <br><br>
<div id="status"></div>
</body>
</html>

函数ajax_post(){
document.getElementById(“status”).innerHTML=“处理…”;
log($(“#用户名”).val());
$.post(“server.php”,{username:$(“#username”).val()},函数(数据){
$(“#状态”).html(数据);
})
}
Ajax发布到PHP并获取返回数据
用户名:



Server.php

<?php 
if(!empty($_POST['username'])){
        $data = $_POST['username'];
        echo 'You are now registered,' . $data;
        $fname = "save.text";

        $file = fopen($fname, 'a');
        fwrite($file, $data . "\n");
        fclose($file);
}
?>


您的浏览器控制台说什么?您还没有包括jQuery库,您应该看到有关$.post行的错误,并且该$未定义。未捕获引用错误:$未定义ajax_post@(索引):6 onclick@(索引):15(索引):6谢谢,它成功了:D.我不知道我必须下载jquery javascript文件并包含它,但现在它工作得非常好!你的浏览器控制台说什么?你还没有包括jQuery库,你应该看到关于$.post行的一个错误,$未定义未捕获引用错误:$未定义ajax_post@(索引):6 onclick@(索引):15(索引):6谢谢,它成功了:D.我不知道我必须下载jquery javascript文件并包含它,但现在它工作得非常好!这也起到了作用,不过我想我不必下载jquery文件。谢谢这也起到了作用,不过我想我不必下载jquery文件。谢谢