views:

192

answers:

4

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"); 
    }
}
A: 

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

Langdon
Yeah nothing gets printed. What should I be doing to remedy this? (PHP newbie)To answer your question button provides a better way of consistent styling (apart from IE6). Check out the buttons on Google or twitter. I'm doing a similar thing. http://stopdesign.com/eg/buttons/3.0/code.html
Katundu
A: 

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%5Fbutton.asp

Rick Mogstad
+1  A: 

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;
cletus
Thanks, I understand, this worked.So I understand and learn, why change the other section of code, what is the advantage?
Katundu
The advantage is twofold: 1) your code exactly matches your intent (in that you only care if the button was pressed, not its value) and 2) brevity (the less code you write to achieve the same thing, the better it is generally speaking).
cletus
Even better, include a `hidden` field to signal that the form is being submitted. You don't generally want to rely on the submit-control giving you a value, because of quirks about when that control is submitted, and what the value is when it's a button.
bobince
A: 
  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

vsr