Jump to content

Recommended Posts

Hey guys.

Im having some problems with a piece of code that Im using to update a value in a field.

What I have happening now, is you select a radio button, click on the update button, and it takes you to a new page.  One of my values (PC Type) I have setup in a drop down box.  When they go to update a PC though, the dropdown box automatically goes to the first value in the selection list.

My question:  How do I code it so that when they goto the update page, the PC Type that was listed initially is the same and not the first value on the selection?

Also while I am asking questions.  When they update a pc, I have a status field and a few of them are like "In Progress" or "Being Built".  But, when they go to update the field, it only shows up until the first part, "In" or "Being".  Is there a way to make it output the whole string?
Link to comment
https://forums.phpfreaks.com/topic/35690-selectoption-listing/
Share on other sites

<?
$computer = $_POST['cboPC'];

$q = "SELECT id, desc FROM table";
$result = mysql_query($q);

$select = "<select name=cboPC>";
for ($i=0;$i<mysql_num_rows($result);$i++) {
  if (mysql_result($result,$i,"id") == $computer) {
  $select .= "<option value=".mysql_result($result,$i,"id")."selected>".mysql_result($result,$i,"desc")."</option>";      } else {
$select .= "<option value=".mysql_result($result,$i,"id").">".mysql_result($result,$i,"desc")."</option>";
}
}
$select .= "</select>";

echo $select;
?>
Link to comment
https://forums.phpfreaks.com/topic/35690-selectoption-listing/#findComment-169096
Share on other sites

[code]<select name=pctype>
<?php
$pctypes = array("HP", "DELL", "COMPAQ");
foreach($pctypes as $pc){
  if($pc == $_POST['pctype']){
    echo "<option selected>$pc</option>";
  } else {
    echo "<option>$pc</option>";
  }
}
?>
</selected>[/code]

Also in PHP make your you put your values in single quotes if you have spaces in the value. That most likely is the problem with your status issue.

Ray
Link to comment
https://forums.phpfreaks.com/topic/35690-selectoption-listing/#findComment-169097
Share on other sites

Thanks for the help.  Ive tried to implement all of what you guys have shown me, still cant seem to get it to work though.

Ill post the scripts, sorry for the length.
My first 'sorta large' php project, so be gentle on any constructive criticism.

[b]index.php[/b]
[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styles.css">
<title>PC Overview</title>

</head>

<body>
<a href="add_pc.php">Add a PC</a><br>
<a href="delete_pc.php">Remove a PC</a><br>
<br><br>

<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("PCLab", $con);
$result = mysql_query("SELECT * FROM desktops");

echo "<form action=update.php method='post'>";
echo "<input type='submit' value='Update Selected'><br><br><table class=tealtable border=1 rules=rows cellpadding=3 cellspacing=1 frame=box>";
echo "<th>Update</th><th>Type</th><th>Name</th><th>Asset #</th><th>Service Tag</th><th>Status</th><th>Date Built</th><th>Lab Tech.</th><th>Lab Location</th><th>Deployed To</th><th>Tech.</th><th>Notes/Comments</th>";

while($row = mysql_fetch_array($result))
{
  echo "<tr><td><input type='radio' value=".$row['ID']." name='pcid' checked='true'></td><td name='pctype'>".$row['PC_TYPE']."</td><td>".$row['PC_NAME']."</td><td>".$row['PC_ASSET']."</td><td>".$row['PC_SERTAG']."</td><td>".$row['PC_STATUS']."</td><td>".$row['PC_BLDDATE']."</td><td>".$row['PC_BLDTECH']."</td><td>".$row['PC_LABLOC']."</td><td>".$row['PC_DEPLOYED']."</td><td>".$row['PC_BYWHO']."</td><td>".$row['PC_NOTES']."</td></tr>";
}

echo "</table><br><input type=submit value='Update Selected'></form>";

mysql_close($con);
?>


</body>
</html>
[/code]

[b]update.php[/b]
[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styles.css">

<title>Update PC</title>
</head>
<body>

<?php

$id=$_POST['pcid'];
$type=$_POST['pctype'];
$con = mysql_connect("localhost","root","");
if(!$con)
{
  die('Could not connect: '.mysql_error());
}

mysql_select_db("PCLab", $con);
$result = mysql_query("SELECT * FROM desktops WHERE ID=".$id."");

$row = mysql_fetch_array($result);

echo "<br><a href='index.php'>Return to Main</a><br>";
echo "Selected: ".$id."<br>";
echo "<form action='update_result.php' method='post'><input type='hidden' name='id' value=".$id."><table>";
?>
<tr><td>Type</td><td><select name='type'>
<?php
    $pctypes = array("GX260","GX270","GX270MT","GX280","GX280MT","GX620","Precision 360","Precision 370","Precision 380","Precision 390");
foreach($pctypes as $pc)
{
  if($pc == $type)
{ echo "<option selected>".$pc."</option>"; }
else
{ echo "<option>".$pc."</option>"; }
}
?> </select></td></tr>

<?php
//echo "<tr><td>Type</td><td><input type='text' name='type' value=".$row['PC_TYPE']."></td></tr>";
echo "<tr><td>Name</td><td><input type='text' name='name' value=".$row['PC_NAME']."></td></tr>";
echo "<tr><td>Asset #</td><td><input type='text' name='asset' value=".$row['PC_ASSET']."></td></tr>";
echo "<tr><td>Service Tag</td><td><input type='text' name='service' value=".$row['PC_SERTAG']."></td></tr>";
echo "<tr><td>Status</td><td><input type='text' name='status' value=".$row['PC_STATUS']."></td></tr>";
echo "<tr><td>Date Built</td><td><input type='text' name='built' value=".$row['PC_BLDDATE']."></td></tr>";
echo "<tr><td>Lab Tech.</td><td><input type='text' name='labtech' value=".$row['PC_BLDTECH']."></td></tr>";
echo "<tr><td>Lab Location</td><td><input type='text' name='location' value=".$row['PC_LABLOC']."></td></tr>";
echo "<tr><td>Deployed To</td><td><input type='text' name='deployed' value=".$row['PC_DEPLOYED']."></td></tr>";
echo "<tr><td>Tech</td><td><input type='text' name='bywho' value=".$row['PC_BYWHO']."></td></tr>";
echo "<tr><td>Notes/Comments</td><td><textarea cols='40' rows'6' name='notes' value=''>".$row['PC_NOTES']."</textarea></td></tr>";
echo "<tr><td><input type='submit' value='Submit Changes'></td></tr>";
echo "</table></form>";

mysql_close($con);
?>


</body>
</html>
[/code]

[b]update_results.php[/b]
[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styles.css">

<title>Update PC</title>
</head>
<body>

<?php

$id=$_POST['pcid'];
$con = mysql_connect("localhost","root","");
if(!$con)
{
  die('Could not connect: '.mysql_error());
}
mysql_select_db("PCLab", $con);

mysql_query("UPDATE desktops SET PC_TYPE='".$_POST['type']."', PC_NAME='".$_POST['name']."',PC_ASSET='".$_POST['asset']."', PC_SERTAG='".$_POST['service']."', PC_STATUS='".$_POST['status']."',PC_BLDDATE='".$_POST['built']."', PC_BLDTECH='".$_POST['labtech']."', PC_LABLOC='".$_POST['location']."',PC_DEPLOYED='".$_POST['deployed']."', PC_BYWHO='".$_POST['bywho']."', PC_NOTES='".$_POST['notes']."' WHERE ID='".$_POST['id']."'");

if(!mysql_query)
{
  echo 'No changes made<br>';
echo "<a href='index.php'>Return to Main</a><br>";
}
else
{
  echo 'Computer updated<br>';
echo "<a href='index.php'>Return to Main</a><br>";
}

mysql_close($con);

?>
</body>
</html>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/35690-selectoption-listing/#findComment-169120
Share on other sites

OK in your index.php you are not passing the pc_type through your form. The only thing you are sending through is the id. <td name=pctype> is not an input variable.
This is fine, what you need to do is get the existing type from the table once you submit the form.
update.php
[code]<?php
$id=$_POST['pcid'];
$con = mysql_connect("localhost","root","");
if(!$con)
{
  die('Could not connect: '.mysql_error());
}

mysql_select_db("PCLab", $con);
$result = mysql_query("SELECT * FROM desktops WHERE ID=".$id."");

$row = mysql_fetch_array($result);
// here is the current pc type
$type=$row['PC_TYPE'];

echo "<br><a href='index.php'>Return to Main</a><br>";
echo "Selected: ".$id."<br>";
echo "<form action='update_result.php' method='post'><input type='hidden' name='id' value=".$id."><table>";
?>
<tr><td>Type</td><td><select name='type'>
<?php
    $pctypes = array("GX260","GX270","GX270MT","GX280","GX280MT","GX620","Precision 360","Precision 370","Precision 380","Precision 390");
foreach($pctypes as $pc)
{
  if($pc == $type)
{ echo "<option selected>".$pc."</option>"; }
else
{ echo "<option>".$pc."</option>"; }
}
?> </select></td></tr>
[/code]

Try that and see if any other problems

Ray
Link to comment
https://forums.phpfreaks.com/topic/35690-selectoption-listing/#findComment-169164
Share on other sites

Ahh!  That worked awesome.  Thanks.

Now, would I pretty much do the same thing if I wanted to add a combo box to a Location, or to Status?

$type=$row['PC_LOC'];
$type=$row['PC_STATUS'];

etc.?

And obviously the code for the combo box.

I have more questions that dont really have anything to do with the select option thing.  Could I just keep asking questions in this thread, or go ahead and make new ones?

And I answered my own question up there, that worked wonders, thanks :)
Link to comment
https://forums.phpfreaks.com/topic/35690-selectoption-listing/#findComment-169177
Share on other sites

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.