[英]jquery does not pass values to mysql db
I,m trying to pass text box value into mysql database using jquery. 我正在尝试使用jquery将文本框值传递到mysql数据库中。 but nothing seems to work and I cannot figure out what the error. 但似乎没有任何效果,我无法弄清楚错误所在。 Here's my code. 这是我的代码。
index.php 的index.php
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<script>
$(document).ready()
$("btn").click(function() {
$.post("send.php", {"named": $("named").val()}, function(data){
alert("Data: " + data + ");}
})
});
</script>
<div>
<form id="form" method="POST" action="">
<input type="text" id="gname"/></br>
<button id="btn">Set</button>
</form>
</div>
</body>
and send.php 并发送
<?php
mysql_connect("localhost", "root", "") or die("failed to connect");
mysql_select_db("ajax01") or die("failed to select");
if (isset($_GET['named'])) {
$named = mysql_real_escape_string($_POST['named']);
}
//$named = "phptest";
mysql_query("INSERT INTO `variables` (`id` , `name`) VALUES ('' , '" . $named . "')");
?>
You are sending data from POST 您正在从POST发送数据
$.post("send.php", {"named": $("named").val()}
and checking if GET is set: 并检查是否设置了GET:
if (isset($_GET['named'])) {
And retreiving the param from $_POST: 并从$ _POST检索参数:
$named = mysql_real_escape_string($_POST['named']);
Hope you got the error now... 希望你现在得到了错误...
Try: 尝试:
if (isset($_POST['named'])) {
This should work 这应该工作
Try this in index.php 在index.php中尝试一下
$.post("send.php", {"named": $("#gname").val()}
and 和
alert("Data: " + data );}
Id is accessed using #
Change this : 使用#
来访问ID:
<script>
$(document).ready()
$("#btn").click(function() {
$.post("send.php", {"named": $("#gname").val()}, function(data){ //updated
alert("Data: " + data + ");}
})
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.