PHP variables inside HTML code

I am doing a student registration form using html and php.

First you will be asked to insert your name, password and email address, and when you click the "Submit" button, you will be redirected to another page ( ChooseDepartment.php) where you will receive a list of all departments from my database to select your own.

Now, I'm a complete newbie, so here is the part of my PHP code that I stuck in SelectDepartment.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 a switch value to transfer the value of the department ID, so I can update my database with the student department, which is currently NULL, but I can’t figure out how to use both html and php correctly on the same line! This gives me a syntax error!

+4
3

PHP, PHP tag.

, , , .

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

, PHP , :

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

echo </form>;

form. ,

echo '</form>';

, .

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

}

, :)

$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>';
}
+3

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="' . $row['DEPT_ID'] .'">';
        echo "<br />";

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

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

php , :

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

Source: https://habr.com/ru/post/1613986/


All Articles