我在PHP中写过这样的东西
<script type="text/javascript" src="jquery.js"></script></script>
<script type="text/javascript">
function get(){
$.post('output.php', { name: form.name.value },
function(output) {
$('#age').html(output).show();
}) ;
}
</script>
<body>
<p>
<form name="form">
id:
<input type ="text" name="name"><input type ="button" name="Submit_1" value ="Get
Existing Data" onclick="get();">//gets info from DB
<p>url: <input type="text" name="calc_url"/></p>
<option value="abc">abc</option>
</select><br/>
<input type="submit" name="Submit_2" value="submit" />
</form>
<div id="title"></div>
</p>
</body>
</html>
Output.php
<?php
Connect to DB
$name = mysql_real_escape_string($_POST['name']);
if(isset($_POST['submit1'])){
if ($name==NULL)
echo "please enter an id!";
else
{
$age= mysql_query("SELECT title FROM parentid WHERE id ='$name'");
$age_num_rows = mysql_num_rows($age);
if ($age_num_rows==0)
echo "id does not exist";
else
{
$sql ="SELECT * FROM parentid WHERE id = '$name'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$abc_output= "Existing data of the record <br />";
$abc_output .="Title: " . $row['title'] . "<br />" ;
$abc_output .="Report No: " . $row['reportno'] . "<br />" ;
$abc_output .="URL: " . $row['calc_url'] . "<br />" ;
$abc_output .="Institution: " . $row['institution'] . "<br />" ;
}
}
echo $abc_output;
}
}
if (isset($_POST['Submit_2'])) {
$value = mysql_real_escape_string($_POST['name']);
$value1 = mysql_real_escape_string($_POST['title']);
$sql = " UPDATE parentid SET title='$value1' WHERE id ='$value'";
if (!mysql_query($sql))
{
die ('Error:' .mysql_error());
}
}
?>
这就是我所做的,它不起作用!! 但如果上面是单独完成的,即没有if(isset())它可以工作(单个按钮单独考虑)
答案 0 :(得分:7)
您可以通过检查提交按钮的存在来检测已按下的提交按钮:
if (isset($_POST['Submit_2'])) {
// "Submit_2" submit button was clicked
}
修改:
if(isset($_POST['name'])
而非if(isset($_POST['submit1'])
,因为参数名称为name
,由$.post()
jquery方法的第二个参数指示:$.post('output.php', { name: form.name.value },
jquery.post
告诉我们查询字符串(在POST或GET请求中)是从该方法的第二个参数构建的。这意味着$_POST
超全局只有一个键值对。通过在PHP脚本的顶部写print_r($_POST)
来测试。form.name.value
。相反,通过引用它的id(最佳方法)来获取DOM对象的值。例如,如果您的输入按钮具有<input ... id="button1" />
,则使用$('#button1').val()
尝试这样的事情:
<form>
<input type="text" name="name" value="">
<input type="button" name="submit1" value="Submit1" id="g1" />
<input type="button" name="submit2" value="Submit2" id="g2" />
</form>
<script type="text/javascript" src="jquery.js"></script></script>
<script type="text/javascript">
$('input[name^="submit"]').click(function() {
submit_name = $(this).attr('name')
submit_value = $(this).val()
$.post('output.php', { submit_name: submit_value },
function(output) {
// do something
});
});
</script>
从来没有真正提交过表单,但它是通过ajax发送的,因此没有submit
类型的输入。