NoooSmyth
NoooSmyth

Reputation: 91

PHP form not posting

This form is not posting values at all and I can't figure out. I have tried using both PHP_SELF and a separate page action but nothing works. All other forms work - so its not a server issue. What am I doing wrong? Thanks.

 <form name=\"projects\" action=\"upload.php\" method=\"POST\">

    <?php 
        echo "<tr>
            <td valign = \"top\" bgcolor=\"$row_color\"> $title </td> 
            <td valign = \"top\" bgcolor=\"$row_color\"> $description </td> 
            <td valign = \"top\" bgcolor=\"$row_color\"> $academicdiscipline </td>
            <td valign = \"top\" bgcolor=\"$row_color\"> "; 

                $allowed = $academicdiscipline; 
                switch ($allowed) {
                case "Civil and Environmental": 
                  echo "<select name=\"priority\" class=\"priority\">
                          <option>  </option>
                          <option value=\"1\"> 1 </option>
                          <option value=\"2\"> 2 </option>
                          <option value=\"3\"> 3 </option> 
                          <option value=\"4\"> 4 </option>
                          <option value=\"5\"> 5 </option>
                          <option value=\"6\"> 6 </option>
                          <option value=\"7\"> 7 </option>
                          <option value=\"8\"> 8 </option>
                          <option value=\"9\"> 9 </option>
                          <option value=\"10\"> 10 </option>
                        </select> ";
                  break;
                case "Electronics": 
                  echo "<select name=\"priority\" class=\"priority\">
                          <option>  </option>
                          <option value=\"1\"> 1 </option>
                          <option value=\"2\"> 2 </option>
                          <option value=\"3\"> 3 </option> 
                          <option value=\"4\"> 4 </option>
                          <option value=\"5\"> 5 </option>
                        </select>";
                  break;
                case "Environment and Management": 
                  echo "<select name=\"priority\" class=\"priority\">
                          <option>  </option>
                          <option value=\"1\"> 1 </option>
                          <option value=\"2\"> 2 </option>
                          <option value=\"3\"> 3 </option> 
                          <option value=\"4\"> 4 </option>
                          <option value=\"5\"> 5 </option>
                        </select>";
                  break;
                case "Mechanical and Materials": 
                  echo "<select name=\"priority\" class=\"priority\">
                          <option>  </option>
                          <option value=\"1\"> 1 </option>
                          <option value=\"2\"> 2 </option>
                          <option value=\"3\"> 3 </option> 
                          <option value=\"4\"> 4 </option>
                          <option value=\"5\"> 5 </option>
                          <option value=\"6\"> 6 </option>
                          <option value=\"7\"> 7 </option>
                          <option value=\"8\"> 8 </option>
                          <option value=\"9\"> 9 </option>
                          <option value=\"10\"> 10 </option>
                          <option value=\"11\"> 11 </option>
                          <option value=\"12\"> 12 </option>
                          <option value=\"13\"> 13 </option> 
                          <option value=\"14\"> 14 </option>
                          <option value=\"15\"> 15 </option>
                        </select> ";
                  break;
                default: 
                    echo "<select name=\"priority\" class=\"priority\">
                          <option>  </option>
                          <option value=\"1\"> 1 </option>
                          <option value=\"2\"> 2 </option>
                          <option value=\"3\"> 3 </option> 
                          <option value=\"4\"> 4 </option>
                          <option value=\"5\"> 5 </option>
                        </select> ";
                  break;
                default: 
                    echo "<select name=\"priority\" class=\"priority\">
                              <option>  </option>
                              <option> 1 </option>
                              <option> 2 </option>
                              <option> 3 </option> 
                              <option> 4 </option>
                              <option> 5 </option>
                            </select>"; } 


            echo "</td></tr>
         <tr bgcolor=\"#ffffff\"> </tr>
         <tr bgcolor=\"#902C2E\"> </tr>"; 

      $row_count++; }  

 echo"<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td ><input name=\"reset\" type=\"reset\" value=\"Reset\"/> 
       <input name=\"submit\" type=\"submit\" value=\"Select\"/> 
</tr></table>" ;  ?>
</form> 

The upload.php page can upload all other entries but the priorities, which I can't get to POST from the code above. This is the upload.php file:

if(isset($_POST[submit]) && !empty($_POST[submit])) { 

$fullname = ''.$data[0]["sn"][0].', '.$data[0]["givenname"][0].'';  
$givenname = ''.$data[0]["givenname"][0].''; 
$priority = '$_POST[priority]'; 

$sql = "SELECT * FROM flux_student_records WHERE studentname = '$fullname' ";
$retval = mysql_query($sql) or die(mysql_error());  

 if (!$retval) { 
        die('Could not retrieve data: There are no project proposals yet. <br /><br /> ' . mysql_error());   }

    while ($row = mysql_fetch_array($retval)) { 
        extract($row);  }

    $sql = "INSERT INTO flux_project_selection( id, studentname, title, academicdiscipline, priority)
            SELECT flux_student_records.id, flux_student_records.studentname, project_proposals.title, project_proposals.academicdiscipline, '$priority'
            FROM project_proposals
            JOIN flux_student_records ON project_proposals.academicdiscipline = flux_student_records.academicdiscipline
            WHERE flux_student_records.studentname = '$studentname'
            AND 'priority' IS NOT NULL
            LIMIT 5"; 

    $retval = mysql_query($sql) or die('Error: MySQL query failed. Project has not been added to the database. '); 

echo "<p> Your project selection have been added to the system. <br> You'll be redirected to list of projects in the system. </p><br />";   
//echo "<meta http-equiv=Refresh content=2;url=index.php>";


} 

Upvotes: 2

Views: 88

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Far as I can tell from your posted code in your question, you're not in PHP with

<form name=\"projects\" action=\"upload.php\" method=\"POST\">

so remove the slashes \ from all of those.

Then these:

if(isset($_POST[submit]) && !empty($_POST[submit])) { 

$priority = '$_POST[priority]'; 

you need to add and remove quotes:

if(isset($_POST['submit']) && !empty($_POST['submit'])) { 

$priority = $_POST['priority']; 

Also, all your <option>'s have no values set for them, so you may want to do something about that.

I.e.: <option value=\"1\"> 1 </option>

etc.

Plus, drop using mysql_ functions. They're deprecated and will be removed from future PHP releases.

Use mysqli with prepared statements, or PDO with prepared statements. They're much safer.


I have tried using both PHP_SELF...

You can use action="" if NOT inside PHP, which does the same as self. Or, action=\"\" if inside PHP, or action='' depending on how you're echoing it.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Edit:

Another thing I noticed in your query:

$sql = "INSERT INTO flux_project_selection( id, studentname, title, academicdiscipline, priority)
        SELECT flux_student_records.id, flux_student_records.studentname, project_proposals.title, project_proposals.academicdiscipline, '$priority'
        FROM project_proposals
        JOIN flux_student_records ON project_proposals.academicdiscipline = flux_student_records.academicdiscipline
        WHERE flux_student_records.studentname = '$studentname'
        AND 'priority' IS NOT NULL
        LIMIT 5"; 

Try removing the quotes from '$priority' and from AND 'priority' since that looks to be a column.

If $priority contains any characters that MySQL may complain about, then wrap it in ticks and not quotes and escaping the variable using mysql_real_escape_string().

I.e.:

project_proposals.academicdiscipline, `$priority`
            FROM project_proposals

...

AND `priority` IS NOT NULL

This does not help you also

$retval = mysql_query($sql) or die('Error: MySQL query failed. Project has not been added to the database. '); 

use or die(mysql_error()); instead of or die('Error: MySQL query failed. Project has not been added to the database. ');

Upvotes: 5

Ehab Eldeeb
Ehab Eldeeb

Reputation: 720

edit

<form name=\"projects\" action=\"upload.php\" method=\"POST\">

to

<form name="projects" action="upload.php" method="POST">

or put it inside the echo

also

$priority = '$_POST[priority]';

this will put $_POST[priority] as a string in the variable $priority

change it to this:

$priority = $_POST['priority'];

Upvotes: 1

Related Questions