3

I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the quote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.

I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.

On form submit, I include the php file that submits this data to the database:

<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
    <label> <br />What did they say?: <br />
    <textarea name="quotetext" rows="10" cols="26"></textarea></label>
    <input type="submit" value="Submit!" />
</form>

The variable $name comes from this (which populates my dropdown menu):

echo "<select name='name'>";

    while ($temp = mysql_fetch_assoc($query)) {
    echo "<option>".htmlspecialchars($temp['name'])."</option>";
    }                       
echo "</select>";

And here is my formsubmit.php:

<?php:
    mysql_select_db('quotes');
    if (isset($_POST['quotetext'])) {
            $quotetext = $_POST['quotetext'];                                   
            $ident = 'yankees';
            $sql = "INSERT INTO quote SET 
        quotetext='$quotetext',
        nametext='$name',
        ident='$ident',
        quotedate=CURDATE()";
        header("Location: quotes.php");
    if (@mysql_query($sql)) {
    } else {
        echo '<p> Error adding quote: ' . 
            mysql_error() . '</p>';
    } 
    }
?>
6
  • what does the form action resolve to when the html is rendered? Commented Jun 13, 2011 at 18:10
  • what are u including in the form tag action ? Commented Jun 13, 2011 at 18:13
  • @G molvi: I'm including my formsubmit.php which I want to be executed when I submit the form. This seems to happen properly except name doesn't get passed at all. Commented Jun 13, 2011 at 18:18
  • see the code below and you were not getting the value of name out of the $_POST array. check below my reply. Commented Jun 13, 2011 at 18:20
  • you really dont have to include the whole php file in the form action tag if you want to remain on this page after page submit then include the php code at the top of the same file and leave the action='' Commented Jun 13, 2011 at 18:22

2 Answers 2

2

Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.

edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):

file 1:

<form action="formsubmit.php" method="post">
    <label> <br />What did they say?: <br />
    <textarea name="quotetext" rows="10" cols="26"></textarea></label>
    <select name='name'>
    <?php
    while ($temp = mysql_fetch_assoc($query)) {
        echo "<option>".htmlspecialchars($temp['name'])."</option>";
    }
    ?>
    </select>
    <input type="submit" value="Submit!" />
</form>

formsubmit.php:

<?php
    mysql_select_db('quotes');
    if (isset($_POST['quotetext'])) {
        $quotetext = $_POST['quotetext'];
        $name = $_POST['name'];
        $ident = 'yankees';
        $sql = "INSERT INTO quote SET 
                quotetext='$quotetext',
                nametext='$name',
                ident='$ident',
                quotedate=CURDATE()";
        if (@mysql_query($sql)) {
            header("Location: quotes.php");
        } else {
            echo '<p> Error adding quote: ' . 
                mysql_error() . '</p>';
        } 
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

your form action="" should be set to a file it's going to submit to, in this case, it should be form action="formsubmit.php". Move $name = $_POST['name'] to the line below where you're setting $quotetext and test.
I think this issue is coming down to not having the correct code structure to submit a form to a php page. See my edits, hopefully they'll help clarify what you should be doing here. The issue with name IS that it's not being set in your formsubmit.php, so the fact that the previous fix didn't work suggests that your code structure's a bit off.
1
echo "<select name='name'>";

    while ($temp = mysql_fetch_assoc($query)) {
     $nyme = htmlspecialchars($temp['name']);
    echo "<option value='$nyme'>$nyme</option>";
    }                       
echo "</select>";-

This way you will receive the value of the name in $_POST array

and you have to get that value out of $_POST array as well you need to change the code add the following line to get the name in your script.

$name = $_POST['name'];

you need to change the form action tag

<form action='formsubmit.php' .....>

and in that file after successful insertion you can redirect the user to whereever.php. so it was fun explaining you every thing bit by bit change this now in your code as well.

if (@mysql_query($sql)) {
          header("Location: quotes.php");

} else {
    echo '<p> Error adding quote: ' . 
        mysql_error() . '</p>';
} 

5 Comments

i have fixed the code try now. and what are u including in the form action tag?
@G molvi: What's "nyme"? Also, I tried this... still nothing.
@G molvi: Okay, so I've tried the following. My action is now: <form action="formsubmit.php" method="post"> and at the top of formsubmit.php I have: $name = $_POST['name']; But now absolutely nothing comes into my database.
this is because you redirect the user to the page before the sql statement to execute. i have updated the code ..try now
@G molvi good catch on the header before mysql_query, didn't even notice that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.