Jump to content

Recommended Posts

I have searched for a solution and have seen that it is possible using javascript but the examples that i have seem to not work with my exact code.

Could somebody assist me with getting this task done. All of the values from my form pass into the database except those with a drop down list.

 

Hello, I have been searching for a long while on how to get the values from my drop down list and none of the methods that i have seen actually work with my code... :v:

 

I have read that it is possible only using javascript? but i have also seen people say that it doable with php? i have tried both methods and i still cannot get the values to be entered into my database

(All of the other fields enter except the fields that i have to select using a drop down

 

Here is my code...

 

<? 
echo'<br/>
<div>
		<p class="addjob">Add Job Form</p>
		<form class="job_form" name="job_form" action="do_add_job.php" method="POST">
			<table class="data">
				<tr>
					<td>Job Name</td>
					<td>
						<input type="text" id="job_name" name="job_name" size="20"></td>
						<td ALIGN=CENTER>

				<tr>
					<td>Company Name</td>
					<td>

					<select name="Select Company Name">'; 
					echo '<option value="">--Select Company Name--</option>'; 
					while($opt = mysql_fetch_array($result)) 
						{ 
					echo '<option value="' . $opt['company_name'] . '"></option>'; 
						} 
					echo'</select> 

					$num
					<td id="fnamev" class="vn"></td>';



					echo'<tr>
					<td>Start Date</td>
					<td>';
					echo'<input type="text" id="start_date" name="start_date" size="10" )></input>

					<a href="javascript:NewCssCal(\'start_date\',\'ddmmyyyy\')"> 
					<img src="sample/images/cal.gif" width="16" height="16" alt="Pick a date"></a>';	

					echo'</td>

					</tr>

					<tr>
					<td>End Date</td>
					<td>

					<input type="text" id="end_date" name="end_date" size="10" )></input>

					<a href="javascript:NewCssCal(\'end_date\',\'ddmmyyyy\')"> 
					<img src="sample/images/cal.gif" width="16" height="16" alt="Pick a date"></a>

					</td>
					</tr>

				<tr>
					<td>Contractor for this job</td>
					<td>
						<SELECT id="contractor" name="contractor" >
						<option value="">--Select Contractor--</option>
						<OPTION VALUE=>James</OPTION>
						<OPTION VALUE=>John</OPTION>
						<OPTION VALUE=>Jim</OPTION>
						</SELECT>
					</td>
				</tr>

				<tr>
					<td>Contracted Hours</td>
					<td>
						<input type="text" id="contracted_hours" name="contracted_hours" size="5" ></input>
					</td>
				</tr>
				<tr>

					<td>Price</td>
					<td>
						<input type="text" id="job_price" name="job_price" size="10" ></input>
					</td>
				</tr>

				<tr>

					<td>VAT</td>
					<td>
					<input type="text" id="vat" name="vat" size="10" ></input>
					</td>
				</tr>

				<tr>
					<td>Status</td>
						<td>
							<SELECT id="job_status" name="job_status">
								<option value="">--Select--</option>
								<OPTION VALUE="ns">Not Started</OPTION>
								<OPTION VALUE="o">Open</OPTION>
								<OPTION VALUE="c">Completed</OPTION>
							</SELECT>
						</td>
				</tr>

				<tr>
					<td>Cost of Job</td>
					<td>
						<input type="text" id="job_cost" name="job_cost" size="10" ></input>
					</td>
				</tr>

				<tr>
					<td>Job Created By</td>
					<td>
						<SELECT id="created_by" name="created_by">
							<option value="">--Select--</option>
							<OPTION VALUE=>John</OPTION>
							<OPTION VALUE=>Jim</OPTION>
							<OPTION VALUE=>James</OPTION>
						</SELECT>						
					</td>
				</tr>

				<tr>
					<td>Notes</td>
					<td>
						<TEXTAREA id="notes" name="notes"  rows=4 size="110")></TEXTAREA>
					</td>
				</tr>

				<tr>
					<td>
						<button type="submit1">SUBMIT</button>
						<class="spacer">
					<td>
				</tr>

			</table>
			<br/>
		</form>
		</div>
	<html>
	<head>
	<link href="forms_css.css" rel="stylesheet" type="text/css"  />
	</head>
	<body>
	</body>
	</html>';

		?>

 

And my do_add_job.php code

 

<?

if ((!$_POST[job_name])  || (!$_POST[notes]) )
	{ header( "Location: add_client_form.php");
	exit;
	} 
else {

$db_name = "";

$table_name = "job";

$connection = @mysql_connect("localhost", "", "")
or die(mysql_error());

$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$sql = "INSERT INTO $table_name
(job_name, FK_company_id, date_started, date_finished, FK_contractor_id, hours, price, vat, job_status, created_by, cost, notes) 
VALUES ('$_POST[job_name]', '$_POST[company_name]', '$_POST[start_date]','$_POST[end_date]', '$_POST[contractor]', '$_POST[contracted_hours]', '$_POST[job_price]', '$_POST[vat]', '$_POST[job_status]', '$_POST[created_by]', '$_POST[job_cost]', '$_POST[notes]')";

$result = @mysql_query($sql, $connection) or die(mysql_error());

header("Location:adminpage.php");
exit;
}

?>

<HTML>
<HEAD>
<TITLE>add client</TITLE>
</HEAD>
<BODY>

 

Any help would be greatly appreciated

 

Link to comment
https://forums.phpfreaks.com/topic/151803-how-do-i-get-values-my-drop-down-list/
Share on other sites

This is in no way a JavaScript issue. You are POSTing values to a PHP page to process. Unless you created JavaScript to somehow interfere with that process JS plays no part in this. There are many things you could have done to debug this problem, such as doing a print_r() on the POSTed values which would have shown that several of your select field were being posted - but had no values! That would have made you take a closer look at your SELECT lists where you created empty values for each option!

 

Here's one example.

<SELECT id="created_by" name="created_by">
<option value="">--Select--</option>
<OPTION VALUE=>John</OPTION>
<OPTION VALUE=>Jim</OPTION>
<OPTION VALUE=>James</OPTION>
</SELECT>

 

After reviewing each select field in the form and how it is used in the processign page, this is what I found:

 

On the form you have a field called "Select Company Name", but in the processing page you use "Company Name". Names need to be consistent.

 

 

The field "contractor" on the form does not have values for the options.

 

The field "job_status" on the form does have values and is included in the processing page. Don't know why this would not be included in the database record from the information provided. Maybe the field is set up an an int or somethign like that. However, all the fields you reference on the processing page reference the POST variable names within quotes like this: $_POST['job_status']. To prevent conflict with the quotes int he string use curly braces around the variables. Example:

VALUES ('{$_POST['job_name']}', '{$_POST['company_name']}',

 

The field "created_by" also has no values for the options

 

Please don't take this as being mean - we all make these types of mistakes. I'm just baffled why you would think this has anything to do with JavaScript. And, I wanted to give an insight into how you can solve your problems on your own or at least get more info about the problem before posting.

This is in no way a JavaScript issue. You are POSTing values to a PHP page to process. Unless you created JavaScript to somehow interfere with that process JS plays no part in this. There are many things you could have done to debug this problem, such as doing a print_r() on the POSTed values which would have shown that several of your select field were being posted - but had no values! That would have made you take a closer look at your SELECT lists where you created empty values for each option!

 

Here's one example.

<SELECT id="created_by" name="created_by">
<option value="">--Select--</option>
<OPTION VALUE=>John</OPTION>
<OPTION VALUE=>Jim</OPTION>
<OPTION VALUE=>James</OPTION>
</SELECT>

 

After reviewing each select field in the form and how it is used in the processign page, this is what I found:

 

On the form you have a field called "Select Company Name", but in the processing page you use "Company Name". Names need to be consistent.

 

 

The field "contractor" on the form does not have values for the options.

 

The field "job_status" on the form does have values and is included in the processing page. Don't know why this would not be included in the database record from the information provided. Maybe the field is set up an an int or somethign like that. However, all the fields you reference on the processing page reference the POST variable names within quotes like this: $_POST['job_status']. To prevent conflict with the quotes int he string use curly braces around the variables. Example:

VALUES ('{$_POST['job_name']}', '{$_POST['company_name']}',

 

The field "created_by" also has no values for the options

 

Please don't take this as being mean - we all make these types of mistakes. I'm just baffled why you would think this has anything to do with JavaScript. And, I wanted to give an insight into how you can solve your problems on your own or at least get more info about the problem before posting.

 

Hello there and thank you for the response  :) (I only started learning php last week so am quite the noob) I now see why i was unable to get the values (silly mistake on my part).

 

* Regarding the Select Company Name.... This was left blank because the name that need to populate this have to come from the database (Because the 'user' will be selecting a adding a job for a company that is already in the database e.g. "SELECT company_name FROM company"). Do you know how i would go about this?

 

That is weird because i have looked around for the majority of the day and a lot of answers suggested using javascript :S oh well!

 

Thanks for your help

 

 

 

* Regarding the Select Company Name.... This was left blank because the name that need to populate this have to come from the database (Because the 'user' will be selecting a adding a job for a company that is already in the database e.g. "SELECT company_name FROM company"). Do you know how i would go about this?

 

Well, your form has a generally correct format for creating the list, but I don't see where you actually run the query. Plus, you are not populating the value into the text of the option. Also, if the records int he company table have IDs, you should be using that as the value of the option. lastly, you would want to include that value in the processing page to include a value for the companyID into the new record being created.

 

This is just an example for the form page

<select name="Select Company Name">
<option value="">--Select Company Name--</option>
<?php
$query = "SELECT id, company_name FROM company_table";
$result = mysql_query($query) or die (mysql_error());
while ($record = mysql_fetch_assoc(result))
{
    echo "<option value=\"{$record['id']}\">{$record['company_name']}</option>\n";
}
?>
</select>

Ahh thanks mate thats awesome! The query wasn't there as i took it out of the code before i posted (sorry). I didn't know how to 'populate' the next bit properly and have just been trying to understand how it would work from random bits of code (but thanks again!).

 

I understand why you say that it should be the ID that they should be selecting but...

 

I thought (from a user point of view) if you want to add a job for a company it would be easier to find the company by the company name: (James n Son) than either numbers (ID:12312) or letters (ID:JAS)???

 

Yes regarding adding the ID to the new table ( the once the company name is selected on the "do_add_job script" that will actually add the POST's to the database i am going to try and have a 2nd sql query that "SELECTS company_id FROM company WHERE company_name = '{$record[company_name]}'" the id returned will go into the jobs table as a foreign key under FK job_id field)

 

Does that sound ok?

 

Also could you just have a quick look at how i have set out my quotes for the calendar input? As everything seems to show up fine (textbox/small-calendar image) but when clicked it does not pop up with the calendar to select a date (So i'm guessing its to do with the quotes because its situated in a PHP script? and is not calling the 'NewCssCal' function)

 

I even tried putting the href into a variable but am stil unable to get it to work... :S

 

<? 
[color=green] $OPENcalendar = "javascript:NewCssCal(\'todays_date\',\'ddmmyyyy\')" ;[/color]
echo'<br/>
<div>

		<p class="addquote">Add Quote Form</p>
		<form class="center" name="newq" action="do_add_quote.php" method="post">
			<table class="data">
				<tr>
					<td>Todays Date</td>

					<td> 
						<input type="text" id="todays_date" name="todays_date" size="5" )></input>

						[color=green]<a href=' . $OPENcalendar . '>
						<img src="sample/images/cal.gif" width="16" height="16" alt="Pick a date"></a>';[/color]
					echo'</td>
				</tr>
				<tr>

				<td>';

					echo'<input type="text" id="hours2" name="hours" size="10" )></input>

					[color=red]<a href="javascript:NewCssCal(\'hours2\',\'ddmmyyyy\')"> 
					<img src="sample/images/cal.gif" width="16" height="16" alt="Pick a date"></a>';[/color]
				echo'</td>
				</tr>';

			</table><br/>
		</form>
		</div>

	<html>
	<head>
	<link href="forms_css.css" rel="stylesheet" type="text/css"  />
	<script type="text/javascript" src="datetimepicker_css.js"></script>
	<script type="text/javascript" src="jquery.js"></script>
	<link href="rfnet.css" rel="stylesheet" type="text/css"  />
	<script language="javascript" type="text/javascript" src="datetimepicker.js">

//Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
//Script featured on JavaScript Kit (http://www.javascriptkit.com)
//For this script, visit http://www.javascriptkit.com

</script>

	</head>
	<body>
	</body>
	</html>';
		?>

 

It's late and I'm not up to reviewing the code, but I will answer this question

 

I thought (from a user point of view) if you want to add a job for a company it would be easier to find the company by the company name: (James n Son) than either numbers (ID:12312) or letters (ID:JAS)

 

You are correct which is why the code I provided will present the suer a list of Companies by name BUT the values of those items will be the company IDs!

Regarding your calendar error, that is a different problem and should have been created as a different post in the forum.

 

However, did you check the actual HTML output of the script to see if the code is being generated correctly with the quotes? I ran a test with just the bit of code you tried to highlight in red and received the following output which "looks" correct to me - but I don't know the correct parameters for your function, so it is only a guess

<input type="text" id="hours2" name="hours" size="10" )></input>
<a href="javascript:NewCssCal('hours2','ddmmyyyy')"> 
<img src="sample/images/cal.gif" width="16" height="16" alt="Pick a date"></a>

 

So, assuming the output looks correct, what DOES happen when you click the link? Do you get an error message? If there is a JS error, there should be a yellow triangle in the bottom left of the window in IE (just double click it to see the error).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.