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

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 »

Just do it and I'm pretty sure it will work.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post 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.
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 »

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>
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post 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.
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 »

You need to change it so that the form loaded with Ajax is not placed inside of the first form.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post 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?
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 »

Yes, I think that'll work. Try it.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post 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.
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 »

Sounds like it's mostly working. You probably just need to clear your Firefox cache.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post by crazitrain02 »

I can always work on compatibility later, but now it's not POSTing the variables from the form. Any ideas?
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's because the form in get_inventory.php uses the POST method.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post 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?
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 »

Er, do you want to POST or GET? Just change the method attribute of the form in get_inventory to whichever one you want.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Get and Post Headache

Post 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.
Post Reply