jquery中获取到值,怎么转到到php查询语句
2个回答
展开全部
用jquery ajax就可以查
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Quick JQuery Ajax Request</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- include the jquery lib -->
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var ajaxSubmit = function(formEl) {
// fetch where we want to submit the form to
var url = $(formEl).attr('action');
// fetch the data for the form
var data = $(formEl).serializeArray();
// setup the ajax request
$.ajax({
url: url,
data: data,
dataType: 'json',
success: function() {
if(rsp.success) {
alert('form has been posted successfully');
}
}
});
// return false so the form does not actually
// submit to the page
return false;
}
</script>
</head>
<body>
<form method="post" action="process.php"
onSubmit="return ajaxSubmit(this);">
Value: <input type="text" name="my_value" />
<input type="submit" name="form_submit" value="Go" />
</form>老友腔
</body>
</html>
process.php
<侍衫?php
function post($key) {
if (isset($_POST[$key]))
return $_POST[$key];
return false;
}
// 数据库连告姿接结
$cxn = mysql_connect('localhost', 'root', '123456');
if (!$cxn)
exit;
mysql_select_db('your_database_name', $cxn);
// check if we can get hold of the form field
if (!post('my_value'))
exit;
// let make sure we escape the data
$val = mysql_real_escape_string(post('my_value'), $cxn);
// lets setup our insert query
$sql = sprintf("INSERT INTO %s (column_name_goes_here) VALUES '%s';",
'table_name_goes_here',
$val
);
// lets run our query
$result = mysql_query($sql, $cxn);
// setup our response "object"
$resp = new stdClass();
$resp->success = false;
if($result) {
$resp->success = true;
}
print json_encode($resp);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Quick JQuery Ajax Request</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- include the jquery lib -->
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var ajaxSubmit = function(formEl) {
// fetch where we want to submit the form to
var url = $(formEl).attr('action');
// fetch the data for the form
var data = $(formEl).serializeArray();
// setup the ajax request
$.ajax({
url: url,
data: data,
dataType: 'json',
success: function() {
if(rsp.success) {
alert('form has been posted successfully');
}
}
});
// return false so the form does not actually
// submit to the page
return false;
}
</script>
</head>
<body>
<form method="post" action="process.php"
onSubmit="return ajaxSubmit(this);">
Value: <input type="text" name="my_value" />
<input type="submit" name="form_submit" value="Go" />
</form>老友腔
</body>
</html>
process.php
<侍衫?php
function post($key) {
if (isset($_POST[$key]))
return $_POST[$key];
return false;
}
// 数据库连告姿接结
$cxn = mysql_connect('localhost', 'root', '123456');
if (!$cxn)
exit;
mysql_select_db('your_database_name', $cxn);
// check if we can get hold of the form field
if (!post('my_value'))
exit;
// let make sure we escape the data
$val = mysql_real_escape_string(post('my_value'), $cxn);
// lets setup our insert query
$sql = sprintf("INSERT INTO %s (column_name_goes_here) VALUES '%s';",
'table_name_goes_here',
$val
);
// lets run our query
$result = mysql_query($sql, $cxn);
// setup our response "object"
$resp = new stdClass();
$resp->success = false;
if($result) {
$resp->success = true;
}
print json_encode($resp);
?>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
多种传递方式
1. $.get 可以传数历递参数
2.$.post 可以链毕碧传递参数棚举
3. .....
Ajax 请求
$.ajax([options])
load(url, [data], [callback])
$.get(url, [data], [fn], [type])
$.getJSON(url, [data], [fn])
$.getScript(url, [callback])
$.post(url, [data], [fn], [type]
1. $.get 可以传数历递参数
2.$.post 可以链毕碧传递参数棚举
3. .....
Ajax 请求
$.ajax([options])
load(url, [data], [callback])
$.get(url, [data], [fn], [type])
$.getJSON(url, [data], [fn])
$.getScript(url, [callback])
$.post(url, [data], [fn], [type]
追问
那用$.get 必须是form表单吗,什么值可以传值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询