类型为“提交”的按钮元素PHP 挑战
最近,我在表单中从 更改为
基本上我所做的就是改变这个:
<input type="submit" name="submitAdd" value="Ask Question! " />
对此:
<button type="submit" class="btn" name="submitAdd"><span><span>Ask Question!</span></span></button>
这是基本的 PHP 处理代码:
//Extract question from submission $question = (isset($_POST["question"]))?$_POST["question"]:""; $question_date = (isset($_POST["question_date"]))?$_POST["question_date"]:""; $submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:""; //Open connect to database include("include/session.php"); //Prepare data for submission $db_question = addslashes($question); $db_question_date = addslashes($question_date); //If form has been submitted, insert question into database if ($submitAdd) { $sql ="INSERT INTO questions (question,question_date) VALUES ('$db_question', '$db_question_date')"; $result = mysql_query($sql); if (!$result) { $message = "Failed to add question. MySQL said " . mysql_error(); } else { header("Location:http://localhost/grill/register.php"); } }
Recently I change from <input type="button">
to <button>
in my forms however the form being processed by PHP wouldn't then submit to the database. Am I missing something in my code?
Basically all I have done is changed this:
<input type="submit" name="submitAdd" value="Ask Question! " />
To this:
<button type="submit" class="btn" name="submitAdd"><span><span>Ask Question!</span></span></button>
Here is the basic PHP processing Code:
//Extract question from submission $question = (isset($_POST["question"]))?$_POST["question"]:""; $question_date = (isset($_POST["question_date"]))?$_POST["question_date"]:""; $submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:""; //Open connect to database include("include/session.php"); //Prepare data for submission $db_question = addslashes($question); $db_question_date = addslashes($question_date); //If form has been submitted, insert question into database if ($submitAdd) { $sql ="INSERT INTO questions (question,question_date) VALUES ('$db_question', '$db_question_date')"; $result = mysql_query($sql); if (!$result) { $message = "Failed to add question. MySQL said " . mysql_error(); } else { header("Location:http://localhost/grill/register.php"); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不起作用,因为按钮版本没有价值。你的代码说:
但是你有:
将其与:
它具有 value 属性进行比较。该值将传递给 PHP 脚本,并且是您正在测试的值。您的
如果
$submitAdd
没有值,即使单击,其值也将为 ''。当您执行此操作时,空字符串的计算结果为 false:因此,我建议进行一些更改。首先,将其更改为:
因为
您并不真正关心该值。
其次,与此无关但仍然值得一提的是,我会这样做:
It doesn't work because the button version has no value. Your code says:
but you have:
Compare this to:
which has a value attribute. This value is passed to the PHP script and is what you're testing. Your
<button>
doesn't have one.With no value
$submitAdd
, even when clicked, will have a value of ''. An empty string evaluates to false when you do this:So, a couple of changes I would recommend. Firstly, change this:
to
since you don't really care about the value.
Secondly, unrelated to this but still worth mentioning, I would do this:
submitAdd
输入字段标记:
Javascript:
不过我不推荐这样做,因为您将依赖 javascript 来提交表单。但即使是 gmail 也是这样做的:P
submitAdd
input field hiddenMarkup:
Javascript:
I wouldn't recommend it though as you would depend on javascript for form submission. But even gmail does it that way :P
我猜测
为什么要从输入更改为按钮?我建议添加一个名为
form_action
的隐藏字段或类似的内容,并将您的 if 逻辑基于该...if ($_POST['form_action'])
而不是if ($submitAdd)
。I'm guessing
<button>
tags don't get submitted, and thus don't end up in $_POST. Just do this at the top of your script to verify:Why did you change from input to button anyway? I would recommend adding a hidden field called
form_action
or something similar and base your if logic on that...if ($_POST['form_action'])
instead ofif ($submitAdd)
.Per w3schools.com
重要提示:如果您在 HTML 表单中使用按钮元素,不同的浏览器将提交不同的值。 Internet Explorer 将提交 和 标签之间的文本,而其他浏览器将提交 value 属性的内容。使用 input 元素在 HTML 表单中创建按钮。
http://www.w3schools.com/tags/tag_button.asp
Per w3schools.com
Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the and tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.
http://www.w3schools.com/tags/tag_button.asp
有同样的问题,我的解决方案不使用 javascript。基本上和用户vsr之前的回答是一样的。
添加包含您想要提交的数据的隐藏输入:
以其他方式命名该按钮:
该按钮将提交表单,并且您想要的数据将位于隐藏输入中,并且可以很好地进一步提交。希望有帮助!
Had the same problem, and my solution uses no javascript. It's mainly the same as user vsr previously answered.
Add a hidden input with the data you want to sumbit:
Name the button somehow else:
The button will submit the form, and the data you want will be in the hidden input, and nicely submitted further. Hope it helps!