Page 1 of 1

Select a single job number rather than just the last

Posted: Wed Aug 12, 2009 3:43 am
by nd3_2000
Hi Guys,
I am working on a job page that uses an sql query to get details of all today's jobs, this then places them all onto a page, then when I click the button under each individual job it passes that value from a (will be but not currently for bugging purposes) hidden text box and passes that value onto a new page to get all details for that particular job.

The problem I am having is that when I click on a button, it automatically passes the last job through, no matter which job I click on (and I know the job no is in the text boxes as I can see them laid out correctly, this is driving me nuts,

I have tried BOTH The post and get methods and a mixture of the two but it always does the same thing, continually passes through the last job number

I will post the code below for the sending page and the receiving page.

First page

Code: Select all

 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>
 
<body>
<form action="GetSingleJob.php" name="GetJobDets" method="POST">
 
 
<?php
$database_name="";
$database_password="";
$database_host="";
$Username="";
 
 
$conn = @mysql_connect($database_host, $Username, $database_password, $database_name) or die(mysql_error());
 
echo "<html><head><title>All Jobs</title></head><body>";
echo "<h1>All Jobs</h1>";
 
$sql = "SELECT * FROM Jobs where Print_Complete=0 and JobNo like '%EB%'";
 
$results=mysql_db_query($database_name,$sql,$conn);
while ($output=mysql_fetch_array($results)) {
 
$JobNo =$output[JobNo];
 
echo "<p><b>Job ID:</b> ". $output[JobNo];
echo "<br /><b>Desc:</b> ". $output[Job_Desc];
echo "<br /><b>Print Date:</b> ". $output[Print_Date];
echo "<br /><b>Delivery Date:</b> ". $output[Delivery_Date];
echo "<br />";
echo("<INPUT TYPE=\"text\"   NAME=\"JobNumber\" VALUE= \"$JobNo\" id='JobNumber'>"); 
echo("<INPUT TYPE=\"submit\"  NAME=\"ViewJob\" VALUE=\"View Job\">"); 
$JobNo="";
echo "<hr /></p>";}
 
mysql_close($conn);
?> 
 
</form>
<INPUT TYPE=BUTTON  NAME='ViewTodaysJobs' VALUE='View Todays Jobs' onClick="window.location='selectatodaysjobs.php'">
<INPUT TYPE=BUTTON  NAME='ViewALLJobs' VALUE='Refresh Jobs' onClick="window.location='selectalljobs.php'">
 
 
 
</body>
</html>
 
Receiving page

Code: Select all

 
<?php
 
   
   
$database_name="";
$database_password="";
$database_host="";
$Username="";
 
 
$conn = @mysql_connect($database_host, $Username, $database_password, $database_name) or die(mysql_error());
 
$JobNo = $_POST['JobNumber'];
//This receives the JobNo data from the hidden field on the previous page
 
$sql = "SELECT * FROM Jobs where JobNo='$JobNo'";
 
 
$results=mysql_db_query($database_name,$sql,$conn);
 
 
echo "<html><head><title>Job No: $JobNo</title></head><body>";
echo "<h1>Job No: $JobNo </h1>";
 
 
while ($output=mysql_fetch_array($results)) {
 
$JobNo =$output[JobNo]; 
 
echo "<p><b>Job ID:</b> ". $output[JobNo];
echo "<br /><b>Date Added:</b> ". $output[Date_Added];
echo "<br /><b>Company Name:</b> ". $output[Company_Name];
echo "<br /><b>Desc:</b> ". $output[Job_Desc];
echo "<br /><b>Press:</b> ". $output[Press];
echo "<br /><b>Print Date:</b> ". $output[Print_Date];
echo "<br /><b>Delivery Date:</b> ". $output[Delivery_Date];
echo "<br />";
echo("<INPUT TYPE=text  NAME='Job' VALUE= $JobNo>"); 
echo "<hr /></p>";
 
 
}
 
mysql_close($conn);
?> 
 
<INPUT TYPE=BUTTON  NAME='ViewALLJobs' VALUE='View ALL Jobs' onClick="window.location='selectalljobs.php'">
 
<INPUT TYPE=BUTTON  NAME='ViewTodaysJobs' VALUE='Todays jobs' onClick="window.location='selectatodaysjobs.php'">
 
<INPUT TYPE=BUTTON  NAME='Complete' VALUE='Job Complete' onClick="window.location='selectalljobs.php'">
 
</form>
 
Many thanks guys a big gold star for anyone who can fix this for me

Re: Select a single job number rather than just the last

Posted: Wed Aug 12, 2009 5:34 am
by robnet
The problem you're having is that everything is in one form. All your submit buttons do the same - they submit this one form. Only, all your input fields have the same name, "JobNumber", so whenever you create a new input field it overwrites the previous value held by the browser. Therefore hitting submit should always return the last value.

You could solve it by creating your form within the while loop so each input / submit pair is in its own form. Or use checkboxes instead of input / submit pairs so you would check the box(es) then hit a 'master' submit button.

Re: Select a single job number rather than just the last

Posted: Wed Aug 12, 2009 5:58 am
by nd3_2000
Hi Rob
cheers for your comments, any way you could give me an example of putting the checkbox in there and how to make that work?

Am new to all this PHP and struggling a bit here!

Re: Select a single job number rather than just the last

Posted: Wed Aug 12, 2009 6:12 am
by robnet
No worries. Thinking about it you'd be better off with radio buttons since this will prevent more than one being checked at once.

Try replacing line 36 with this:

Code: Select all

 echo "<INPUT TYPE='radio'  NAME='JobNumber' VALUE='$JobNo'>" ;
And add your submit line after your while loop (But before the </form> tag) - line 39ish.


Once you're happy with it you can remove line 35 all together - you wont need this, even hidden, as all the info you need is in the radio button input.

Re: Select a single job number rather than just the last

Posted: Wed Aug 12, 2009 6:21 am
by nd3_2000
Cheers Rob, that worked pretty well thanks for your input

Re: Select a single job number rather than just the last

Posted: Wed Aug 12, 2009 7:43 am
by robnet
Sorry. There's a much more simple way to do your form - back to the initial design you wanted.

You can use name and values within your submit input too, so rather than having radio buttons and having to click twice, just have a submit button where you originally had them that looks like this:

Code: Select all

echo"<INPUT TYPE="submit"  NAME="$JobNo" VALUE="View Job">";
Then in your receiving page you need to define $JobNo slightly differently:

Code: Select all

foreach ($_POST as $key => $value){
 if($value=='View Job'){
  $JobNo=$key;
 }
}
Hope that works better for you!
Rob