0

I am very new to HTML and PHP. I am trying to create a drop down menu to insert a selection into my database. I have been trying things for hours to get this to work. I have a form a client needs to fill out for an evaluation and they can only select from jobs that exist in the database already.

The evaluation table has the JobNo, evalDate, raterName, rating, and comment (optional). The JobNo is found in the jobs table as jobNo (lowercase j in the jobs table, uppercase in evaluation)

Bonus points if you can get it to fill data from the database as the drop down box choices. I tried that for forever too.

Please tell me what I am missing or doing wrong?

<!DOCTYPE html>
<html>
<body>
 <body style="background-color:#FAEBD7;">
<title>CTS Employment Agency | Evaluation</title>  <!-- title for tab on website -->

<center><h1 style="background-color: #FAEBD7;">CTS Employment Agency</h1></center>
<center><p>123 Anywhere St.<br>Huntsville, AL 35649<br>P: (256)555-5555<br>Fax:(256)554-5554<br>E-mail: questions@ctsemployment.com</p></center>
<center><h3>Evaluation Form</h3></center>

<center><p>Please take a moment to fill out the evaluation form concerning the worker that was provided to you.<br>
Please leave a rating from 1(unsatisfactory) to 5(excellent) for the worker along with a comment box for additional information.
<br> If you have any questions, please feel free to send us an email or call us!</p></center>

<form action="php_formStore.php" method="$Post_"> <!-- beginning of form -->
 <br>
 <center>

Worker First Name: <input type="text" name="wFName">

Middle: <input type="text" name="wMinit">

Last: <input type="Text" name="wLName">

SSN <input type="text" name="snn" placeholder="000-00-0000">
<br><br>


 Job No: 
<select name="JobNo">
  <option value="46">46</option>
  <option value="47">48</option>
  <option value="48">48</option>
</select>

//<input type="text" name="JobNo">  //this works fine, why doesn't the select box work?


 Worker's Job Title: <input type="text" name="title">

Rating: <input type="text" name="rating" placeholder="Rate from 1-5">
<br><br>    

Evaluator's Name: <input type "text" name="raterName">

Evaluation Date:   <input type="date" name="evalDate">
     <br><br>  

 <!-- comment box -->
<textarea name="comment"  rows="8" cols="60"> 
(Optional)
Leave any additional comments here. 
</textarea><br>

<input type="submit" value="Submit">  <!-- The submit buttom -->
<input type="reset">
</center>
</form>


</body>
<html>

and here is the PHP file

<?php
$servername = "localhost";

$username = "root";

$password = "";

$db="cts employment agency";  //Database Name 
$con=mysqli_connect($servername, $username, $password, $db);

//check connection
if(mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

       $Job_No = $_GET['JobNo'];
       $raterName = $_GET['raterName'];
       $evalDate = $_GET['evalDate'];
       $rating = $_GET['rating'];
       $comment = $_GET['comment'];

       echo "<br><br><br><br>";
       echo "<center><h2>Thank you for taking the time to fill out this evaluation<br>";
       echo "Your feedback helps us to provide you with the highest quality employees<br>";

       echo "If you have any questions, please e-mail us at: questions@ctsemployment.com or call: (256)555-5555</h2></center>";

      mysqli_query($con, "INSERT INTO evaluation (JobNo, evalDate, raterName, rating, comment)
      VALUES ('$Job_No', '$evalDate', '$raterName', '$rating', '$comment')");

      mysqli_close($con);
?>



  [1]: https://i.stack.imgur.com/2mJGC.png
Bella
  • 17
  • 4

3 Answers3

0

In your php script you are trying to access $_POST vars with $_GET.

If you want to use post as suggested on your form, change the declaration to:

 <form action="php_formStore.php" method="post"> <!-- beginning of form -->

Then in your php script, change your $_GET vars to $_POST like so:

   $Job_No = $_POST['JobNo'];
   $raterName = $_POST['raterName'];
   $evalDate = $_POST['evalDate'];
   $rating = $_POST['rating'];
   $comment = $_POST['comment'];

Your code is also highly at risk from SQL injection. You should research the use of prepared statements to counteract this. A guide to this can be seen below:

https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

This is crucial in keeping data integrity and security from unwanted data access.

Juakali92
  • 1,155
  • 8
  • 20
  • Ok thanks, I will try that. I know the code is risky but its for a school project that will never be published. Just written and tested on root. – Bella Dec 03 '19 at 02:07
  • You'll make a much better impression using prepared statements. Minimal effort to implement and learning it is crucial in PHP development – Juakali92 Dec 03 '19 at 02:09
  • I changed everything to post and refreshed the page to try again using some test values and now I am getting an error: Notice: Undefined index: JobNo in C:\xampp\htdocs\php_formStore.php on line 17 Notice: Undefined index: raterName in C:\xampp\htdocs\php_formStore.php on line 18 Notice: Undefined index: evalDate in C:\xampp\htdocs\php_formStore.php on line 19 Notice: Undefined index: rating in C:\xampp\htdocs\php_formStore.php on line 20 Notice: Undefined index: comment in C:\xampp\htdocs\php_formStore.php on line 21 – Bella Dec 03 '19 at 02:10
  • Ok, this is just all we have been taught was $_GET in class – Bella Dec 03 '19 at 02:11
  • I'm guessing you left these fields blank in your form submission? If they are not filled in, they won't be sent as part of the form. You'll need to check if they are set using isset or !empty before trying to use them – Juakali92 Dec 03 '19 at 02:14
  • No, I used test data. I have a dummy job in my database that I used and it populates just fine when jobNo is a text input. It displays all 4 values in the table after hitting submit. – Bella Dec 03 '19 at 02:18
  • Ok hard to see whats going on, I can take a look for you and give you a few pointers. If you send me an email to my email address on my profile i'll try and help. – Juakali92 Dec 03 '19 at 02:21
0

First add [] to your name

Job No: 
<select name="JobNo[]">
  <option value="46">46</option>
  <option value="47">48</option>
  <option value="48">48</option>
</select>

And then to your Php file

$Job_No= "";
    foreach ($_POST['JobNo'] as $select) {
        $Job_No = $select;
    }

To fill data from the database as the drop down box choices, this links may help you

Adding options to select with javascript

How do I pass variables and data from PHP to JavaScript?

Paulo44
  • 211
  • 1
  • 4
  • I must not be placing it in the right section. Now I am getting an error saying Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\php_formStore.php on line 18 – Bella Dec 03 '19 at 02:21
0

       $Job_No= "";
    foreach ($_POST['JobNo'] as $select) {
        $Job_No = $select;
    };
       $raterName = $_POST['raterName'];
       $evalDate = $_POST['evalDate'];
       $rating = $_POST['rating'];
       $comment = $_POST['comment'];

       echo "<br><br><br><br>";
       echo "<center><h2>Thank you for taking the time to fill out this evaluation<br>";
       echo "Your feedback helps us to provide you with the highest quality employees<br>";

       echo "If you have any questions, please e-mail us at: questions@ctsemployment.com or call: (256)555-5555</h2></center>";

      mysqli_query($con, "INSERT INTO evaluation (JobNo, evalDate, raterName, rating, comment)
      VALUES ('$Job_No', '$evalDate', '$raterName', '$rating', '$comment')");

      mysqli_close($con);
Bella
  • 17
  • 4