[英]How to get Textbox value to a php variable within same php file with a button click (without submitting the form)?
I am trying to get the textbox value with a button click (without form submission) and assign it to a php variable within the same php file. 我试图通过单击按钮(不提交表单)获取文本框值,并将其分配给同一php文件中的php变量。 I tried AJAX, but, I don't know where I am making mistake.
我尝试过AJAX,但是,我不知道在哪里出错。 Sample code: File name: trialTester.php
示例代码:文件名:trialTester.php
<?php
if(!empty($_POST))
echo "Hello ".$_POST["text"];
?>
<html>
<head>
<title> Transfer trial </title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<input type="textbox" id="scatter" name="textScatter">
<button id="inlinesubmit_button" type="button">submit</button>
<script>
$(document).ready(function(){
function submitMe(selector)
{
$.ajax({
type: "POST",
url: "",
data: {text:$(selector).val()}
});
}
$('#inlinesubmit_button').click(function(){
submitMe('#scatter');
});
});
</script>
</body>
</html>
$(document).ready(function(){
. $(document).ready(function(){
。 text
parameter, but in your PHP code you're accessing textScatter
. text
参数,但是在您的PHP代码中,您正在访问textScatter
。 Change that to text
text
<input type="text" id="scatter" name="textScatter">
, there is no textbox
type. <input type="text" id="scatter" name="textScatter">
,没有textbox
类型。 - -
function submitMe(selector)
{
$.ajax({
type: "POST",
url: "",
data: {text:$(selector).val()},
success: function(newHtml) {
$("body").html(newHtml);
}
});
When you do $.ajax
you specify the names of the POSTed parameters yourself, name="textScatter"
is not taken into account. 当您执行
$.ajax
您自己指定POSTed参数的名称,不会考虑name="textScatter"
。
By using ajax you can get the post value but by replacing the existing content. 通过使用ajax,您可以获取帖子值,但可以替换现有内容。
See the code below, as all the content is pesent in the body tag, so we will replace the content present in this body through the response of ajax. 请参见下面的代码,因为所有内容都存在于body标记中,所以我们将通过ajax的响应替换此主体中存在的内容。
Copy & paste this code & let me know if you have any query. 复制并粘贴此代码,如果您有任何疑问,请与我们联系。
<?php
if(!empty($_POST))
echo "Hello ".$_POST["text"];
?>
<html>
<head>
<title> Transfer trial </title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<input type="textbox" id="scatter" name="textScatter">
<button id="inlinesubmit_button" type="button">submit</button>
<script>
$(document).ready(function(){
function submitMe(selector)
{
$.ajax({
type: "POST",
url: "",
data: {text:$(selector).val()},
success: function(data){
alert(data);
$("body").html(data);
}
});
}
$('#inlinesubmit_button').click(function(){
submitMe('#scatter');
});
});
</script>
</body>
</html>
Hope, this may be helpful for you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.