在一个按钮上组合JS和PHP。可能吗?
Hiya: 我知道有些人会对我的问题感到厌倦,但我正在做一个uni项目,需要尽快完成。这个问题是关于在按钮(按钮)上使用JS并在同一个按钮上发送php_my_sql更新。问题是JS使用按钮,对吧?但PHP使用按钮(提交)。如何让这两个按钮在其中一个按钮上工作,因为必须只有一个按钮。在一个按钮上组合JS和PHP。可能吗?
这是我的代码JS
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect")
x.remove(x.selectedIndex)
}
</script>
HTML
<form method="post">
<select id="collect" name="Select1" style="width: 193px">
<option>guns</option>
<option>knife</option>
</select> <input type="**submit/button**" onclick="formAction()" name="Collect" value="Collect" /></form>
PHP
<?
if (isset($_POST['Collect'])) {
mysql_query("UPDATE Player SET score = score+10
WHERE name = 'Rob Jackson' AND rank = 'Lieutenant'");
}
?>
这可以是一个方式
提交通过JS的形式除去参数后
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect")
x.remove(x.selectedIndex);
document.forms[0].submit();
}
</script>
输入类型按钮
<input type="button" onclick="formAction()" name="Collect" value="Collect" />
JavaScript可以用而用户该按钮交互被导航页面并将数据输入到表单中。用户立即按下提交按钮,并发送表单提交请求发送JS不再有控制权。该请求被发送到表单的动作(很可能是一个PHP文件),该动作处理请求并给出答案。
如果您确实需要将两者结合起来,请查看AJAX。
好的。谢谢。我试图检查出AJAX,但我不是很擅长 – Kelvin 2011-03-04 13:04:53
如果您需要任何帮助,一定要问这个问题! :) – 2011-03-11 21:56:02
<?php print_r($_POST); ?>
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect");
x.remove(x.selectedIndex);
submit_form();
}
function submit_form() {
document.form1.submit();
}
</script>
<form method="post" name='form1'>
<input type='hidden' name='Collect'/>
<select id="collect" name="Select1" style="width: 193px">
<option>guns</option>
<option>knife</option>
</select> <input type="button" onclick="formAction()" name="Collect" value="Collect" /></form>
<?
if (isset($_POST['Collect'])) {
//do whatever update you want
}
?>
不起作用。这是链接:http://www.thirdworld.vacau.com/table。php – Kelvin 2011-03-04 12:59:39
您的按钮显示了吗?对于这里的标记不太确定''input type =“** submit/button **”',也许你的意思是' erickb 2011-03-04 12:29:56
@erickb:这不是错字,他不确定哪一个在这种情况下使用 – 2011-03-04 12:32:33
你的[全部](http://stackoverflow.com/questions/5193312/clearing-a-selected-item-from-a-dropdown使用PHP)[问题](http://stackoverflow.com/questions/5192575/using-php-to-remove-a-selected-item-from-drop-down-amongst-others)是相同的 – diEcho 2011-03-04 12:33:26