English 中文(简体)
Button Element with Type Submit & PHP Challenges
原标题:

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"); 
    }
}
问题回答

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;
  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

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).

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

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!





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签