New to this forum and to programming in PHP, so apologies if I miss some detail.

I have found some code which does almost everything I need, but I can't figure out how to get the 'selected item' from the results on an secondary page (reading in post) ..

this properly retrieves a list of names from a sql table and populates a select list, and two input boxes

<div id="ifNew" style="display:none" >
            <form name = "form2" action="../Form/modified.php" method = "post" enctype = "multipart/form-data">     
            <div class = "form_data1">    

                     <?php

                     $stmt = sqlsrv_query( $conn,$tsql );
                     if( $stmt === false) {    die( print_r( sqlsrv_errors(), true) );}
                            echo "<select id=''nm'' name=''Name''>";
                                while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
                                    {       echo "<option value='" . $row['Name']. "'>" . $row['Name'] . "</option>"; 
                                    }
                                    echo "</select>"; 


                    sqlsrv_free_stmt($stmt);?>  

                     <input type = "text" name = "group" value = "" size = "30" required/><label> :Group</label> <br>
                     <input type = "text" name = "btype" value = "" size = "30" required/><label> :Type</label><br>
                     <input type = "submit" />
            </form>
                </div>              
            </div>  
        </div>

        What I am struggling with is how to retrieve the selected item from the name list and populate a variable

        modified.php
        <?php
$group = $_POST['group'];
$btype = $_POST['btype'];
$name = $_POST['name'];
?>

while the first two are popuated (group and btype), the name is not, and i receive an error
Notice: Undefined index: name in C:\inetpub\wwwroot\testWeb\Form\modified.php on line 4

please help

Recommended Answers

Your problem appears to be on line 9 of the code you provided:

echo "<select id=''nm'' name=''Name''>";

Instead, try:

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

I'm not sure why you had the two single quotes next to each other, so that needed to be fixed. Also, …

Jump to Post

All 4 Replies

Your problem appears to be on line 9 of the code you provided:

echo "<select id=''nm'' name=''Name''>";

Instead, try:

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

I'm not sure why you had the two single quotes next to each other, so that needed to be fixed. Also, while group and btype are lowercase, you have the name of the select field Name but then you were trying to retrieve $_POST['name'].

Let me know if this works.

I thought the same thing, but I have tried changing it :
echo "<select id="nm" name="name">";
Parse error: syntax error, unexpected 'nm' (T_STRING), expecting ';' or ','

So I changed it to
echo '<select id="nm" name="name">';

And it worked, thank-you very much. I had copied it from a the net and didn't totally understsand how it worked.

A single quote indicates treating everything inside as a literal string. Double quotes indicate parsing out variables inside the string. For example:

$foo = 'World';

echo 'Hello $foo'; // Prints Hello $foo
echo "Hello $foo"; // Prints Hello World

You can use single quotes inside a double-quoted string, or double quotes inside a single-quoted string. But if you're going to try to encase double quotes within double quotes, it is treated as a premature end to the string. You need to escape the quote with a backslash as so:

echo '<select id="nm" name="name">'; // This will work
echo "<select id=\"nm\" name=\"name\">"; // This will work too

Many thanks Dani, I will have more questions.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.