Deena Osman
Deena Osman

Reputation: 61

PHP variables inside HTML Code

I am making a student sign-up form using html and php.

First you are asked to insert your name, password and email and when you click submit, it takes you to another page (ChooseDepartment.php) in which you get a list of all departments from my database to choose your own.

Now, I am a total newbie, so here is the part of my php code that I am stuck with in ChooseDepartment.php:

$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
    echo "<br />"."Available departments: "."  ".mysql_num_rows($ShowPossibleDep)."<br />";
    echo "<br />";
    echo '<form id = "dept" action = "Courses.php" method = "post">';
    while($row = mysql_fetch_array($ShowPossibleDep))
    {
        echo $row['NAME'];
        echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
        echo "<br />";


    }
    echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
    echo </form>;

}

I am trying to make the value of the radio button carry the value of the the department id so I can then update my database with the student's department which is currently NULL, but I can't figure out how to use both html and php at the very same line correctly! This gives me syntax error!

Upvotes: 4

Views: 527

Answers (3)

Mubin
Mubin

Reputation: 4425

as you're in PHP, so you don't need to open and close PHP tag.

The reason you're getting Syntax error is just because you're not manipulating string properly.

error is with this line

echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';              
                                                   ^ here                ^ here

So you need to remove the PHP tags and need to concatenate string properly like:

echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';

and with this one

echo </form>;

you're missing quotes around form tag. So it should be,

echo '</form>';

There are some other typos are as well, so your final code will be look like this.

$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
    echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
    //echo "<br />";  add this <br /> tag to next echo
    echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
    while($row = mysql_fetch_array($ShowPossibleDep))
    {
        echo $row['NAME'];
        echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
        //or you can do this way
        //echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
        //echo "<br />"; appended in upper statement.
    }
    echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
    //echo </form>;    closed already(above statement).

}

and without comments, more cleaner :)

$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
    echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
    echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
    while($row = mysql_fetch_array($ShowPossibleDep))
    {
        echo $row['NAME'];
        echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
    }
    echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}

Upvotes: 3

Mateo Barahona
Mateo Barahona

Reputation: 1391

No need to open php tag again

$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0) {
    echo "<br />"."Available departments: "."   ".mysql_num_rows($ShowPossibleDep)."<br />";
    echo "<br />";
    echo '<form id = "dept" action = "Courses.php" method = "post">';
    while ($row = mysql_fetch_array($ShowPossibleDep)) {
        echo $row['NAME'];
        echo '<input type="radio" name="department" value="' . $row['DEPT_ID'] .'">';
        echo "<br />";

    }
    echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
    echo "</form>";
}

Upvotes: 0

Vincent
Vincent

Reputation: 348

Take a look at string operators

http://php.net/manual/en/language.operators.string.php

You can combine two strings in php with a dot, so that part of your code would become this:

    {
        echo $row['NAME'];
        echo '<input type="radio" name="department" value="'.$row['DEPT_ID'].'">';
        echo "<br />";
    }

Upvotes: 0

Related Questions