Get and Post Headache

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Get and Post Headache

Post by crazitrain02 »

I'm trying to have a form be displayed with information from MySQL, that is populated from an onselect combobox, that is a modify form with a submit button. I have everything working with the exception of the submit post. When I hit the submit button I am getting the error: "Notice: Undefined index: q in C:\Inetpub\wwwroot\crazitrain.com\inventory\get_inventory.php on line 2"
Can someone please help me out and point me in the right direction of what I'm doing wrong here?

modify_inventory.php

Code: Select all

<html>
<head>
<title>Modify Current Inventory</title>
<link rel="stylesheet" href="CSS\standard.css">
</head>
<body align="center">
<h2>Modify Current Inventory</h2>
Select the part number from the drop-down list to modify.
<?php
include 'admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to database');
mysql_select_db($dbname);

$query = "SELECT DISTINCT partnum FROM parts;";

if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
	echo "<form method='post' name='partnum' action='get_inventory.php'>";
    echo "<select name='partnum' onchange='getInventory(this.value);'>\n";
    echo "<option>-- Part Number --</option>\n";
while ($row = mysql_fetch_array($result))
    echo "<option value='$row[partnum]'>$row[partnum]</option>\n";
    echo "</select>\n<p />";
	echo "<div align='center' id='txtHint'></div>";
}
      else { echo "No results found."; }
}
    else { echo "Failed to connect to database."; }
mysql_close();
?>
<script type="text/javascript">
function getInventory(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_inventory.php?q="+str,true);
xmlhttp.send();
}
</script>
<br />
<FORM>
<INPUT TYPE="button" VALUE="Home" onclick="window.location.href='index.php';" />
<INPUT TYPE="button" VALUE="View Inventory" onclick="window.location.href='current_inventory.php';" />
</FORM>
</body>
</html>
get_inventory.php

Code: Select all

<?php
$q=$_GET["q"];
include 'admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to database');
mysql_select_db($dbname,$conn);

$query="SELECT p.partnum, p.quantity, p.description, p.manufacturer, p.model, s.status FROM parts p JOIN status s ON p.statusid = s.statusid WHERE partnum='".$q."'";
$result = mysql_query($query);

$stat_query="SELECT status FROM status ORDER BY statusid";
$stat_result = mysql_query($stat_query);

mysql_close($conn);

echo "
<table width='75%' class='inventory_list'>
<tr>
<th class='desc'>Part Number</th>
<th class='quan'>Quantity</th>
<th class='desc'>Description</th>
<th class='desc'>Manufacturer</th>
<th class='desc'>Model</th>
<th class='quan'>Status</th>
</tr>
<FORM ACTION='modify.php' METHOD='POST'>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['partnum'] . "</td>";
  echo "<td><input type='text' class='quan' name='quan' value=" . $row['quantity'] . " /></td>";
  echo "<td><input type='text' class='desc' name='desc' value=" . $row['description'] . " /></td>";
  echo "<td><input type='text' class='desc' name='man' value=" . $row['manufacturer'] . " /></td>";
  echo "<td><input type='text' class='desc' name='mod' value=" . $row['model'] . " /></td>";
  echo "<td><select><option selected=".$row['status'].">".$row['status']."</option>\n";
while($stat_row = mysql_fetch_array($stat_result))
{  
  echo "<option name='stats' value='".$stat_row[status]."'>$stat_row[status]</option>\n";
}
  echo "</select></td>";
  echo "</tr>";
  }
echo "</table>";
echo "
	<INPUT type='submit' value='Submit Change' />
	<INPUT type='button' value='Cancel' onclick='history.go(-1)' />
</FORM>";
?>
modify.php

Code: Select all

<html>
<head>
<title>Modify Part Inventory</title>
<link rel="stylesheet" href="CSS\standard.css">
</head>
<body>
<?php
include 'admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to SQL Database');
mysql_select_db($dbname);
$stat = "$_POST[status]";
$statquery="SELECT DISTINCT statusid FROM status WHERE status= '".$stat."'";
$result=mysql_query($statquery);
$num=mysql_num_rows($result);
while ($row=mysql_fetch_assoc($result))
{
$statid=$row["statusid"];
}
$insert="UPDATE parts SET quantity='$_POST[quan]', description='$_POST[desc]', manufacturer='$_POST[man]', model='$_POST[mod], statusid='$_POST[stats]')
WHERE partnum='$_POST[partnum]'";

if (!mysql_query($insert,$conn))
	{
	die("Error: ".mysql_error());
	}
echo "Record changed";
mysql_close($conn)
?>
</body>
</html>
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Get and Post Headache

Post by s992 »

crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

If I manually put in http://www.mysite.com/get_inventory.php?q=123456 then the form is filled out properly. That URL is being submitted by the onselect statement of the combo box, and is returning correctly. The issue is when I click the submit button to do the POST command to update the row in the MySQL database.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get and Post Headache

Post by Jonah Bron »

Is the problem with the first form or the second form? By your OP, it sounded like it was with the Ajax request... but now you're saying that the second form submission has problems. Perhaps you could clarify your problem?
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

The way I'm trying to get this to work is to have a combo box that is populated from a table out of MySQL. Once a value is selected from the combo box the javascript function is called and a GET function is performed. When that GET function is performed it is populating a form of text boxes and another combo box.
What I want to happen is for the end user to be able to modify the information within the form and then submit the data via a POST command to another page so the information within that form will update the record in the database.
The error is occurring when I hit the submit button and attempting to POST the data to the modify.php page. Also the error is being displayed on the get_inventory.php page, so it looks like the POST is POSTing to the incorrect page.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get and Post Headache

Post by Jonah Bron »

And what is the error exactly?
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

"Notice: Undefined index: q in C:\Inetpub\wwwroot\crazitrain.com\inventory\get_inventory.php on line 2"
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get and Post Headache

Post by Jonah Bron »

That error is coming from get_inventory.php, which means that the error occurs in the Ajax request. Try replacing...

Code: Select all

xmlhttp.open("GET","get_inventory.php?q="+str,true);
...with...

Code: Select all

var url = "get_inventory.php?q=" + str;
alert(url);
xmlhttp.open("GET",url,true);
...and tell us what the alert box says.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

The alert window comes back with get_inventory.php?q=123456. That portion of what I want to happen is working correctly.
Once that Ajax is called then the page renders this form:

Code: Select all

<FORM ACTION='modify.php' METHOD='POST'>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['partnum'] . "</td>";
  echo "<td><input type='text' class='quan' name='quan' value=" . $row['quantity'] . " /></td>";
  echo "<td><input type='text' class='desc' name='desc' value=" . $row['description'] . " /></td>";
  echo "<td><input type='text' class='desc' name='man' value=" . $row['manufacturer'] . " /></td>";
  echo "<td><input type='text' class='desc' name='mod' value=" . $row['model'] . " /></td>";
  echo "<td><select><option selected=".$row['status'].">".$row['status']."</option>\n";
while($stat_row = mysql_fetch_array($stat_result))
{  
  echo "<option name='stats' value='".$stat_row[status]."'>$stat_row[status]</option>\n";
}
  echo "</select></td>";
  echo "</tr>";
  }
echo "</table>";
echo "
	<INPUT type='submit' value='Submit Change' />
	<INPUT type='button' value='Cancel' onclick='history.go(-1)' />
</FORM>";
When I click on the Submit Change button then I think the page is attempting to re-run that Ajax function instead of POSTing the data in that form to the modify.php page.
Last edited by crazitrain02 on Wed Dec 08, 2010 6:53 pm, edited 1 time in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get and Post Headache

Post by Jonah Bron »

That little change makes it stop working? What does the alert box say? That change only tells you the url being requested.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

Jonah Bron wrote:That little change makes it stop working? What does the alert box say? That change only tells you the url being requested.
Sorry, replaced the wrong section of code. I've updated my prior post with the information.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get and Post Headache

Post by Jonah Bron »

Okay, so you click the "Submit Change" button, and it takes you to modify.php, correct? What do you see there.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

When I click the submit button that is when the error is displayed on the get_inventory.php page. This is where I get confused and need some help. The submit button is POSTing to the get_inventory.php page instead of the modify.php page.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Get and Post Headache

Post by Jonah Bron »

Okay, I think I figured it out. The problem is that you're rendering the second form inside of the first form. You need to take the #txtHint div out of the form.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

That's the functionality that I'm trying to keep is that once you select an item out of the combo box the form is displayed with the information for the item that was selected in the combo box. Then the end user can modify the information as needed, and then submit it to be updated within the database.
Should I be doing this with some type of postback during the onselect for the combo box instead of doing a get function?
Post Reply