类型为“提交”的按钮元素PHP 挑战

发布于 2024-08-12 03:07:10 字数 1289 浏览 5 评论 0原文

最近,我在表单中从 更改为

基本上我所做的就是改变这个:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦亦如幻 2024-08-19 03:07:10

它不起作用,因为按钮版本没有价值。你的代码说:

$submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:"";

但是你有:

<button type="submit" class="btn" name="submitAdd"><span><span>Ask Question!</span></span></button>

将其与:

<input type="submit" name="submitAdd" value="Ask Question! " />

它具有 value 属性进行比较。该值将传递给 PHP 脚本,并且是您正在测试的值。您的

如果 $submitAdd 没有值,即使单击,其值也将为 ''。当您执行此操作时,空字符串的计算结果为 false:

if ($submitAdd) {

因此,我建议进行一些更改。首先,将其更改为:

$submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:"";

因为

$submitAdd = isset($_POST['submitAdd']);

您并不真正关心该值。

其次,与此无关但仍然值得一提的是,我会这样做:

$db_question = mysql_real_escape_string($question);
$db_question_date = mysql_real_escape_string($question_date);
$sql = <<<END
INSERT INTO QUESTIONS (question, question_date)
VALUES ('$db_question', '$db_question_date')
END;

It doesn't work because the button version has no value. Your code says:

$submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:"";

but you have:

<button type="submit" class="btn" name="submitAdd"><span><span>Ask Question!</span></span></button>

Compare this to:

<input type="submit" name="submitAdd" value="Ask Question! " />

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:

if ($submitAdd) {

So, a couple of changes I would recommend. Firstly, change this:

$submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:"";

to

$submitAdd = isset($_POST['submitAdd']);

since you don't really care about the value.

Secondly, unrelated to this but still worth mentioning, I would do this:

$db_question = mysql_real_escape_string($question);
$db_question_date = mysql_real_escape_string($question_date);
$sql = <<<END
INSERT INTO QUESTIONS (question, question_date)
VALUES ('$db_question', '$db_question_date')
END;
镜花水月 2024-08-19 03:07:10
  1. 隐藏 submitAdd 输入字段
  2. 单击按钮时调用 Javascript 函数

标记:

<input type="hidden" name="submitAdd" value="Ask Question! " />

<button type="submit" onclick="submitForm();" class="btn" name="submitAdd">
<span><span>Ask Question!</span></span></button>

Javascript:

function submitForm(){
    document.forms["form_name"].submit();
}

不过我不推荐这样做,因为您将依赖 javascript 来提交表单。但即使是 gmail 也是这样做的:P

  1. Make the submitAdd input field hidden
  2. Call a Javascript function on click of the button

Markup:

<input type="hidden" name="submitAdd" value="Ask Question! " />

<button type="submit" onclick="submitForm();" class="btn" name="submitAdd">
<span><span>Ask Question!</span></span></button>

Javascript:

function submitForm(){
    document.forms["form_name"].submit();
}

I wouldn't recommend it though as you would depend on javascript for form submission. But even gmail does it that way :P

╭ゆ眷念 2024-08-19 03:07:10

我猜测

print $_POST["submitAdd"];

为什么要从输入更改为按钮?我建议添加一个名为 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:

print $_POST["submitAdd"];

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 of if ($submitAdd).

一指流沙 2024-08-19 03:07:10

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

維他命╮ 2024-08-19 03:07:10

有同样的问题,我的解决方案不使用 javascript。基本上和用户vsr之前的回答是一样的。

添加包含您想要提交的数据的隐藏输入:

<input name="submitAdd" type="hidden" value=" Ask Question! ">

以其他方式命名该按钮:

<button type="submit" class="btn" name="submitsForm"> Ask Question! </button>

该按钮将提交表单,并且您想要的数据将位于隐藏输入中,并且可以很好地进一步提交。希望有帮助!

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:

<input name="submitAdd" type="hidden" value=" Ask Question! ">

Name the button somehow else:

<button type="submit" class="btn" name="submitsForm"> Ask Question! </button>

The button will submit the form, and the data you want will be in the hidden input, and nicely submitted further. Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文