php 如何将jquery变量传递给php然后显示它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15833008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass a jquery variable to php and then display it
提问by Abhishek
Problem in passing Jquery variable to php .
将 Jquery 变量传递给 php 的问题。
When i pass variable id to B.php
i am getting this error
当我将变量 id 传递给B.php
我时出现此错误
"Notice: Undefined index: id1 in C:\xampp \htdocs\PhpProject1\OtherUsableItems\B.php on line 2
How to solve this problem ???? This A.php
如何解决这个问题呢 ????这个A.php
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var id = 23;
$("#submit").click(function(){
$.post("B.php",
{
id1: id,
} );
});
});
</script>
</head>
<body>
<form action="B.php" method="post" >
<input type="submit" id="submit" name="submit"/>
</form>
</body>
</html>
This B.php
这个 B.php
<?php
$a =$_POST['id1'];
echo $a ;
?>
I want id variable to pass on to B.php
我希望 id 变量传递给 B.php
回答by premananth
Try this one:
试试这个:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var id = 23;
$("#submit").click(function(){
$.ajax(
{
url: "B.php",
type: "POST",
data: { id1: id},
success: function (result) {
alert('success');
}
});
});
});
</script>
</head>
<body>
<form >
<input type="button" id="submit" name="submit"/>
</form>
</body>
</html>
Then B.php
然后B.php
<?php
$a =isset($_POST['id1'])?$_POST['id1']:'not yet';
echo $a ;
?>
回答by Nisanth Sojan
Try changing the input type to button instead of submit
<input type="button" id="submit" name="submit"/>
尝试将输入类型更改为按钮而不是提交
<input type="button" id="submit" name="submit"/>
回答by Shijin TR
The problem is that your form is submited ,$.post
is for ajax sumit. For that you need to prevent form submit.
问题是您的表单$.post
已提交,适用于 ajax sumit。为此,您需要防止表单提交。
<script>
$(document).ready(function(){
var id = 23;
$("#submit").click(function(){
$.post("B.php",
{
id1: id,
});
event.preventDefault() ;
});
});
</script>
Add event.preventDefault();
in your button click function
添加event.preventDefault();
您的按钮点击功能
回答by Jai
Try this:
尝试这个:
<script>
$(document).ready(function(){
var id = 23;
$("#submit").click(function(e){
e.preventDefault();
$.post("B.php", {id1: id}, function(data){ //<---get the data from B.php
console.log(data); //<--------show it here
alert(data);
});
});
});
</script>
Since you are using ajax feature so you have to stop the form submit feature. You have a trailing comma here {id1 : id,}
this is an issue in IE browsers but latest other browsers will just work fine.
由于您使用的是 ajax 功能,因此您必须停止表单提交功能。这里有一个尾随逗号,{id1 : id,}
这是 IE 浏览器中的一个问题,但最新的其他浏览器可以正常工作。
Note:
笔记:
You posted this error:
您发布了此错误:
"Notice: Undefined index: id1 in C:\xampp \htdocs......
"Notice: Undefined index: id1 in C:\xampp \htdocs......
Don't run the page in filesystem $.post()
won't work this way.
不要在文件系统中运行页面$.post()
不会以这种方式工作。
Instead try using http://localhost/yourapplication/page.php
而是尝试使用 http://localhost/yourapplication/page.php
回答by Viktor S.
$(document).ready(function(){
var id = 23;
$("#submit").click(function(e){
$.post("B.php", {id1: id} );
e.preventDefault();
});
});
Try code above. First:
JS does not allow to do something like this: {id1: id,}
(see comma at the end). That is a feature of PHP. Actually, your JS code should not be executed at all because that is a syntax error (suppose you will find it in console of your browser)
试试上面的代码。第一:JS 不允许做这样的事情:({id1: id,}
见最后的逗号)。这是 PHP 的一个特性。实际上,根本不应该执行您的 JS 代码,因为这是一个语法错误(假设您会在浏览器的控制台中找到它)
Another problem: you must prevent default submit of a form. e.preventDefault()
is needed for that. Without it, ajax post will be started and than usual form submit will happen. And your form has no element with name id1, suppose that second request produce that error message.
另一个问题:您必须防止表单的默认提交。e.preventDefault()
是需要的。没有它,ajax post 将被启动,并且比通常的表单提交会发生。并且您的表单没有名称为 id1 的元素,假设第二个请求产生该错误消息。
EDIT:This is to post a value with regular submit, so browser is redirected to B.php
编辑:这是通过常规提交发布一个值,因此浏览器被重定向到 B.php
$(document).ready(function(){
var id = 23;
$("#submit").click(function(){
$(this).closest("form")
.append($("<input>", {"type":"hidden", "name":"id1", "value":id}));
});
});
回答by Aashray
Use a hidden field to set the value you want to send across. Since you are using submit, your entire form is submitted. The hidden field is also sent and this can be accessed directly using the name of the hidden field.
使用隐藏字段设置要发送的值。由于您使用的是提交,因此您的整个表单都已提交。隐藏字段也被发送,这可以使用隐藏字段的名称直接访问。
回答by fr4nk
An E_NOTICE is thrown by php because the index of the POST-Array doesnt exist. You can switch error_reporting of for notices, see http://www.php.net/manual/de/errorfunc.configuration.php#ini.error-reportingor check if the index exists:
php 抛出 E_NOTICE,因为 POST-Array 的索引不存在。通知的error_reporting可以切换,参见http://www.php.net/manual/de/errorfunc.configuration.php#ini.error-reporting或者检查索引是否存在:
if( isset($_POST['id1']) ) {
// do something
}
回答by Shijin TR
Try this code,
试试这个代码,
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var id = 23;
$("#submit").click(function(){
$('#id1').val(id);
});
});
</script>
</head>
<body>
<form action="B.php" method="post" >
<input type="submit" id="submit" name="submit"/>
<input type="hidden" name="id1" id="id1" value=""/>
</form>
</body>
</html>
Here you can pass value of 'id' to B.php,And will be access as $_POST['id1']
在这里,您可以将 'id' 的值传递给 B.php,并将作为 $_POST['id1'] 访问
回答by Zhama
Or you could use submit() this way
或者你可以这样使用 submit()
$(function(){
var id = 23;
$("#myform").submit(function() {
$.post("B.php", { id1: id }).done(function(data) {
alert( data );
});
return false; //true(post form data)
});
});
And form
和形式
<form id="myform" method="post">
<input type="submit" id="submit" name="submit" />
</form>
EDIT:Oh and yeah for B.php isset check or error_reporting(0);
编辑:哦,是的 B.php isset 检查或 error_reporting(0);
if(isset($_POST['id1'])){
print_r( $_POST );
}