使用 ajax 将文本值传递给另一个 php 文件
Pass text value to another php file using ajax
我编写了一个包含文本框和提交按钮的代码。当我用文本框值输入输入时,它应该使用 ajax 转到另一个页面(q.php),并且它应该从那里返回值到这里(主页.php)这是我的代码。(首页.php)
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","q.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form onsubmit="return myFunction(document.getElementById('fname'))">
Enter name: <input type="text"id="fname" name="fname">
<input type="submit" value="Submit">
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
我的下一页(q.php)
<?php
$q = $_GET['q'];
echo $q."Some Content from this page";
?>
我的答案是使用html+jquery+php
:-
<html>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script><!-- add jquery library -->
<form method="post">
Enter name: <input type="text"id="fname" name="fname">
<input type="submit" value="Submit" class= "abc">
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
<script type = "text/javascript">
$(document).ready(function(){ //when your page is rendered completly
$('.abc').click(function(e){ //when you click on submit button this function will run
e.preventDefault();//to prevent normal submission of form
if($('#fname').val() !== ''){ //check name field have some value
$.post('q.php',{'q':$('#fname').val()},function( data ){ //send value to post request in ajax to the php page
$('#txtHint').html(data); //print the response coming from php page to the result div inside your html code
});
}
});
});
</script>
在 php 文件中:-
<?php
$q = $_POST['q'];
echo $q."Some Content from this page";
?>
相关文章:
- 通过另一个php应用程序将我的项目推送到Github存储库中
- 如何将JavaScript变量的数据传递到另一个php页面
- 将php页面URL中的变量传递给JavaScript和另一个php
- 如何使用javascript/jquery将变量从一个PHP文件传递到另一个PHP文件
- 通过AJAX将从AJAX获取的值发送到另一个PHP文件
- 使用 Ajax 将 Javascript 变量提交到另一个 PHP 文件
- 从 PHP 表中选择值并将其传递给另一个 PHP 页面
- 如何通过选择下拉列表中的选项从另一个 php 文件中获取数据
- 将值从一个 Php 文件传递到另一个 Php 文件
- php中的mysql查询不起作用,但它在另一个php文件中起作用
- 如何在另一个php文件中从ajax获取变量
- 调用另一个php脚本生成图表时出现问题
- 如果jquery脚本's在另一个php页面的php-include语句中使用
- 如何使用wp_localize_script从另一个php文件传递参数
- 将输出从一个 JS 文件传递到另一个 PHP 文件
- 如何包含来自另一个 PHP 文件的 PHP 验证函数
- 在另一个 PHP 文件的 JavaScript 中显示 PHP 文件内容
- 从另一个 php 脚本终止一个脚本
- 如何将 JavaScript 变量传递给另一个 PHP 文件
- 使用 ajax 将文本值传递给另一个 php 文件