繁体   English   中英

如何使用javascript将GET或POST方法的参数发送到php页面

[英]how to send parameter using GET or POST method to php page using javascript

我正在尝试以其他方式发送参数形式file.php(具有用javascript编写的<script> .... </script> )。 我正在尝试将参数传递给其他文件file2.php,但是我没有这样做。 (对不起,我的英语不好)。 这是我正在尝试的代码。

file.php

<html>
<head>
</head>
<body>
<script type="text/javascript">
function changeThis(){

var formInput = document.getElementById('theInput').value;
document.getElementById('newText').innerHTML = formInput;
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "event2.php?theInput"= + formInput, true );
xmlHttp.send();
return xmlHttp.responseText;    
}

 </script>

 <!--  <p>You wrote: <span id='newText'></span> </p> -->
     <textarea id="theInput" style="height:200px;">Write Here</textarea>
    <input type='button' onclick='changeThis()' value='See what you wrote'/>

    </body>
    </html>

file2.php

<?php
  $id = $_GET["theInput"];
  echo $id;
?>

您已注释掉<span id='newText'></span> ,因此document.getElementById('newText')将是undefined因此尝试为document.getElementById('newText').innerHTML赋值document.getElementById('newText').innerHTML将抛出一个异常,您的函数将在此时终止。

停止尝试修改已删除的元素,或者放回该元素。


除此之外,这将成功发送数据(尽管您确实应该对您正在填充到查询字符串中的用户输入进行URL编码)。 它对响应没有任何作用,因为您在XHR对象上没有loadreadystatechange事件处理程序。

您的第一个方法可能会起作用,只需更改即可:

xmlHttp.open( "GET", "file2.php?theInput=" + formInput, true );

错误的:

xmlHttp.open( "GET", "event2.php?theInput"= + formInput, true );

您的php文件名似乎是file2.php而不是event2.php

您必须在“”之间使用=

您的旧脚本已修正:

<html>
<head>
</head>
<body>

<script type="text/javascript">

function changeThis(){
 var formInput = document.getElementById('theInput').value;
 document.getElementById('newText').innerHTML = formInput;
 var xmlHttp = null;
 xmlHttp = new XMLHttpRequest();
 xmlHttp.open( "GET", "file2.php?theInput=" + formInput, true );
 xmlHttp.send();
 return xmlHttp.responseText;     
}

</script>

 <p>You wrote: <span id='newText'></span> </p> 
 <textarea id="theInput" style="height:200px;">Write Here</textarea>
<input type='button' onclick='changeThis()' value='See what you wrote'/>

</body>
</html>

file.php

<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.min.js'></script>

<script type="text/javascript">
function changeThis(){
    $.ajax({
    type: "POST",
    url: "file2.php",
    data: {theInput:theInput},
    dataType: 'json',
    success:function(data){
        $('#newText').html(data.newText);
        }
    });
}
</script>

</head>
<body>

<span id='newText'></span>
<textarea id="theInput" style="height:200px;">Write Here</textarea>
<input type='button' onclick='changeThis()' value='See what you wrote'/>

</body>
</html>

file2.php

<?php
        $arr = array();
        $arr['newText'] = '';

        if(isset($_REQUEST['theInput']))
        {
            $arr['newText'] = $_REQUEST['theInput'];
        }

        die(json_encode($arr));
?>

首先确保event2.php和file2.php是同一文件。 然后尝试使用逃逸。

xmlHttp.open( "GET", "event2.php?theInput="= + encodedURIComponent(formInput), true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM