Jump to content

php generated form


goltoof

Recommended Posts

Why won't this form submit?  When I hit submit the page just stays and doesn't submit anything to the db.  It uses ajax to display the form (test.php) but the problem isn't the ajax just the way the php generates the form, i think.

 

display.php

<?php
$q=$_GET["q"];
if ($q == true)
 	echo '<table><form method="post" action="update.php" onsubmit="return udpate()">';
if ($q == "test") 
	include 'test.php'; 
if ($q == "test2") 
	include 'test2.php'; 
 if ($q == true)
 	echo '</table><input type="submit" value="Add Product" /></form>';
 if ($q == false) 
 	echo '';
?>

 

test.php

<?php
echo '<tr><td>Product Category:</td><td><input type="text" name="prod_cat" size="" /></td></tr>';
echo '<tr><td>Model# </td><td><input type="text" name="model" size="" /></td></tr>';
echo '<tr><td>List Price </td><td><input type="text" name="price_list" size="" /></td></tr>';
?>

 

update.php

<?php
include ('link.php');
$id = $_POST[''];
$prod_cat = $_POST['prod_cat'];
$model = $_POST['model'];
$price_list = $_POST['price_list'];
$query= "INSERT INTO product_specs (ID, 
					 prod_cat, 
					 model, 
					 price_list
					 ) 									 
		VALUES ('NULL',
				'".$prod_cat."',
				'".$model."', 
				'".$price_list."')
				";
		mysql_query($query) or die ('Error updating database: ' . mysql_error());
?>

Link to comment
Share on other sites

What does your update() function look like?

 

It's just a simple alert and page refresh

 

function update()
		{
		alert("Database updated.");
		window.location.reload(true);
		}

 

I don't think that's the problem here though because the html (echoed in php) works fine in it's own page but it doesn't work when created with display.php and test.php

Link to comment
Share on other sites

Just in case, here's index.php, containing the ajax that populates the "getDisplay" div with display.php and test.php:

 

	<script type="text/javascript">
		function getDisplay(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("getDisplay").innerHTML=xmlhttp.responseText;
				}
			}
			xmlhttp.open("GET","display.php?q="+str,true);
			xmlhttp.send();
		}
	</script>
</head>
<body>
	<div id="header">
		<div id="form">
			<form>
				<select onchange="getDisplay(this.value)">
					<option value="">Select product category</option>
					<option value="test">Test</option>
					<option value="test2">Test 2</option>
                                        </select>
                               </form>
                       </div>
	</div>
	<div id="getDisplay">
		<div id="info"><!-- display.php & test.php appear here --!></div>
	</div>

Link to comment
Share on other sites

what does a "view source" in your browser show?

Source looks like this...  When "test" is selected, display.php populates it along with test.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<title>Update</title> 
<link rel="stylesheet" href="style.css" type="text/css"> 
	<script type="text/javascript"> 
		function getDisplay(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("getDisplay").innerHTML=xmlhttp.responseText;
				}
			}
			xmlhttp.open("GET","display.php?q="+str,true);
			xmlhttp.send();
		}
	</script> 
	<script language="javascript"> 
		function update()
		{
		alert("Database updated.");
		window.location.reload(true);
		}
	</script> 
</head> 
<body> 
	<div id="header"> 
		<div id="form"> 
			<form> 
				<select onchange="getDisplay(this.value)"> 
					<option value="">Select product category</option> 
					<option value="select-test">Test</option> 
				</select> 
			</form> 
		</div> 
	</div> 
	<br /> 
	<div id="getDisplay"> 
		<div id="info">Product info listed here.</div> 
	</div> 
</body> 
</html> 

 

Link to comment
Share on other sites

the get display div that is populated is outside of your form tags. therefore the submit is outside your form tags when the html is inserted. I'd imagine thats the issue ?

 

If you look back "test.php" and "display.php" are both set inside of the "getDisplay" div.  So the entire form is intact inside the same div. 

 

There are two forms here, one is the select form which triggers the AJAX to display the form that is created by display.php and test.php.  The AJAX (select) form works fine, and the form inside of the getDisplay div appears fine, but it won't submit.

Link to comment
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.