4

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!

4
  • I strongly believe you should read PHP in w3school first before start using SO. No pun intended. Commented Oct 31, 2015 at 20:11
  • I'm voting to close this question as off-topic because It is of very very low quality. It seems students as asking question to teacher in about basic syntax of a programming language Commented Oct 31, 2015 at 20:15
  • @Imdad: I think we should leave it open, as we all start from asking low quality questions. :) Commented Oct 31, 2015 at 20:20
  • 1
    Potatoes gonna potate! </3 Commented Oct 31, 2015 at 21:04

3 Answers 3

3

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>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for taking the time to help! :)
0

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 />";
    }

Comments

0

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

Comments

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.