Page 2 of 2

Re: Get and Post Headache

Posted: Wed Dec 08, 2010 8:08 pm
by Jonah Bron
Just do it and I'm pretty sure it will work.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 10:57 am
by crazitrain02
I commented out the <div id='txtHint'> tag from the modify_inventory.php page, and now the form is not showing up on the page after I select an item from the combo box. The alert box still comes back with the proper URL.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 11:23 am
by Jonah Bron
No, don't remove it, move it. Here, like this:

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 />";
}
      else { echo "No results found."; }
}
    else { echo "Failed to connect to database."; }
mysql_close();
?>
</form>
<div align="center" id="txtHint"></div>
<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>

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 11:41 am
by crazitrain02
I tried that code change, but now it will not POST the data to the modify.php page.
I also noticed that in my original code that I never closed out the first form with the combo box, but even closing out that form with the second form inside of it doesn't work.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 1:35 pm
by Jonah Bron
You need to change it so that the form loaded with Ajax is not placed inside of the first form.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 1:44 pm
by crazitrain02
Here is my code now for the modify_inventory.php page:

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 />
	</form>";
}
      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>

<div align="center" id="txtHint" />

<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>
This should not be loading the Ajax in the first form correct?

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 2:22 pm
by Jonah Bron
Yes, I think that'll work. Try it.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 2:55 pm
by crazitrain02
In Chrome and IE when I hit the submit button none of the variables are being POSTed to the modify.php page.
In Firefox when I hit the submit button I get the same Undefined index: q error as before.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 3:45 pm
by Jonah Bron
Sounds like it's mostly working. You probably just need to clear your Firefox cache.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 4:03 pm
by crazitrain02
I can always work on compatibility later, but now it's not POSTing the variables from the form. Any ideas?

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 5:08 pm
by Jonah Bron
That's because the form in get_inventory.php uses the POST method.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 5:27 pm
by crazitrain02
Is there another way that I can "post" the data from the form and update it in the MySQL database, or should I try and figure out another way of doing this?

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 5:30 pm
by Jonah Bron
Er, do you want to POST or GET? Just change the method attribute of the form in get_inventory to whichever one you want.

Re: Get and Post Headache

Posted: Thu Dec 09, 2010 5:40 pm
by crazitrain02
I'm an idiot... It is POSTing the data, but just not the one variable that I'm looking for. :banghead:

Thanks for all of your help :mrgreen: I've been looking for days trying to figure this out, but haven't found anything.