使用jquery从div中运行表单(插入/更新/删除)
这是前一个问题的后续问题:“iframe与div+jquery”。我在main.php的div中加载了一个表单(form1.php)。我希望能够在div中运行表单,而无需刷新main.php来填充数据库。 下面是main.php:使用jquery从div中运行表单(插入/更新/删除),jquery,forms,html,Jquery,Forms,Html,这是前一个问题的后续问题:“iframe与div+jquery”。我在main.php的div中加载了一个表单(form1.php)。我希望能够在div中运行表单,而无需刷新main.php来填充数据库。 下面是main.php: <html> <head> <script type="text/javascript" src="javascript/update-div.js"></script> <scri
<html>
<head>
<script type="text/javascript" src="javascript/update-div.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$("#submit_btn").click(function(e) {
e.preventDefault();
$.post('form1.php', $("#form1").serialize(), function(result) {
$("#fc").html(result); });
});
</scrip>
</head>
<body leftmargin="0px" topmargin="0px" marginheight="0px" marginwidth="0px" bgcolor="#FFFFFF">
<a href="javascript:ajaxpage('form1.php','fc')">form1</a>
<br>
<div id="fc"></div>
</body>
</html>
$(文档).ready(函数(){
$(“提交”)。单击(功能(e){
e、 预防默认值();
$.post('form1.php',$(“#form1”).serialize(),函数(结果){
$(“#fc”).html(结果);});
});
这是form1.php的代码
<?php
$link = mysql_connect('localhost', 'login', 'pwd');
if (!$link) {die('Not connected : ' . mysql_error());}
$db_selected = mysql_select_db('db_name');
$name = strtoupper($_POST['name']);
$birth = strtoupper($_POST['birth']);
$act = $_POST['act'];
if($act == "save"){
$query="INSERT INTO `mnl_people` ( `name` , `birth`) VALUES ('$name', '$birth')";
mysql_query($query);
$idalbum=mysql_insert_id();
}
if(!empty($name)){
$html = "<p>Your name: <b>".$name."</b></p>";
$html .= "<p>Your birthplace: <b>".$birth."</b></p>";
echo("$html");
}
else{ ?>
<form name="form1" enctype='multipart/form-data'>
Name <input type='text' name='name' value=""><br>
<br/>
<br/>
Birthplace : <input type="text" name="birth"><br/>
<input type='submit' name='act' class='submit' value='save' id="submit_btn"/>
</form>
<?php
}
?>
使用该版本,因此
$("#submit_btn").click(function(e) {
使用
问题是,当您附加单击处理程序时,表单在DOM中不存在,因为您使用ajax加载它。您没有将表单的方法声明为POST
,因此它使用GET
方法,但您尝试获取POST
值
在表单标记中添加属性method=“POST”
,或使用$\u GET['field\u name']
来检索它。结束脚本标记的输入错误应该是
,而不是
。谢谢。部分工作正常,现在main.php不刷新,只有div被刷新。但是,$name和$birth参数仍然是空的。还有一件事,我在一个网站上读到delegate()比live()更可取.我不记得确切的原因了,而且解释得很透彻(我是一个jquery初学者)。你有什么想法吗??
$("#submit_btn").live('click',function(e) {