[英]send an integer value of a variable from javascript to php file
HTML CODE HTML代码
<form action="send.php" method="post">
<input type="hidden" name="qty" id="quantity" value="">
<input type="submit" id="submit">
</form>
JAVASCRIPT CODE JAVASCRIPT代码
var qty=10;
$("#submit").click(function(){
$("#quantity").val(qty);
});
PHP FILE (send.php) PHP文件(send.php)
<?php
$showqty=$_POST['qty'];
echo $showqty;
?>
My code above is receiving an error on the php file. 我上面的代码在php文件上收到错误。 It says that the index qty is undefined. 它说索引数量未定义。 How can I send an integer value of a variable from javascript to php? 如何从javascript向php发送变量的整数值?
只需使用
var qty = document.getElementById("qty").value
You do not have any issues with you javascript
or you PHP
code. 您的javascript
或PHP
代码都没有任何问题。
when you recieve undefined error, it means that the object is not even posted to the send.php file. 当您收到未定义的错误时,这意味着该对象甚至都没有发布到send.php文件中。 so the problem is with the object or the way you are sending it, not with the value in it. 因此问题出在对象或您发送对象的方式上,而不是其中的值。
with a look at you code I saw that you have two action
tags and no method
tag and that is what causing the error. 通过查看您的代码,我看到您有两个action
标签,没有method
标签,这就是导致错误的原因。
with no method tag, the form uses GET
method instead of POST
method. 没有方法标签的窗体使用GET
方法而不是POST
方法。
so you just have to change this line: 所以你只需要改变这一行:
<form action="send.php" action="post">
with this line: 用这一行:
<form action="send.php" method="post">
action="post">
should be method="post">
action="post">
应该是method="post">
This may help you: 这可以帮助您:
<!doctype html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
var qty=10;
$("#submit").click(function(e){
var qty=10;
$.ajax({
url:'send.php',
type:'POST',
data:{'qty':qty},
success: function(e){
alert(e);
//do some thing
}
});
});
});
</script>
</head>
<body>
<form id="myform" action="send.php" method="post" onSubmit="return false;">
<input type="submit" id="submit">
</form>
</body>
</html>
send.php send.php
<?php
$showqty=$_POST['qty'];
echo $showqty;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.