Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with form posting 1

Status
Not open for further replies.

Coogan

Programmer
Jun 21, 2004
38
GB
Hello,

Can anyone see why this piece of code is not working. Sometimes the page looks as if it is posting the data but nothing appears in the table "request" and then there are other times when i get "Notice: Undefined index: id"

Any suggestions??

Code:
<?
if(isset($_POST['post']))  {
	$name = $_POST['name'];
	$id = $_POST['id'];
			
    $SQL = " INSERT INTO request ";
    $SQL = $SQL . " (name, type, requested_id, completed) VALUES ";
    $SQL = $SQL . " ('$name', 'cd','$id','no') ";

        $result = mysql_db_query($db,"$SQL",$cid);
		echo ("$name");
	
	if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }
    }

if(isset($_POST['post']))  {$id = $_POST['id'];} else {$id = $_GET['id'];}

	$SQL = " SELECT * FROM music ";
    $SQL = $SQL . " WHERE id = $id ";
		
    $ret = mysql_db_query($db,$SQL,$cid);
		
    $row = mysql_fetch_array($ret);
        $artist = $row["artist"];
		$title = $row["title"];
		$id = $row["id"];
		

?>

<form name="form1" method="post" action="requestcd.php"> 
<INPUT TYPE="hidden" NAME="id" VALUE="<? echo("$id"); ?>">

<input name="name" type="text" id="name">

<input type="submit" name="Submit" value="Submit">

</form>

Thanks

Martin

 
you dont actually have a value for $_POST['post'] try $_POST['submit'] instead.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi Karver,

It still comes back with "Notice: Undefined index: id" when posting the form.

Any ideas?

Thanks

Martin
 
Code:
<INPUT TYPE="hidden" NAME="id" VALUE="<? echo("$id"); ?>">
In this line you're trying to output value of $id which is clearly not set anywhere in the script. That is why you get a warning that no value is set.
 
Hi Vragabond,

Sorry I should of explained that the original "id" is passed to the script via the request for the page i.e: "example.php?id=1"

I then belive that the follwoing line should take that number and set it as the "id".

Code:
if(isset($_POST['post']))  {$id = $_POST['id'];} else {$id = $_GET['id'];}

Thanks

Martin
 
Ah I see. It shows I am sleepy. So, which is the line the script sends you the warning for?
 
Ok,

This is where I am at the moment.

I have changed some of the variables in order to try and find out what is going wrong.

The code currently looks like this:

Code:
<?
if(isset($_POST['submit']))  {
	$name = $_POST['name'];
	$id = $_POST['id'];
			
    $SQL = " INSERT INTO request ";
    $SQL = $SQL . " (name, type, requested_id, completed) VALUES ";
    $SQL = $SQL . " ('$name', 'cd','$id','no') ";

        $result = mysql_db_query($db,"$SQL",$cid);
		echo ("$name");
	
	if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }
    }

if(isset($_POST['submit']))  {$id2 = $_POST['id'];} else {$id2 = $_GET['id'];}

	$SQL = " SELECT * FROM music ";
    $SQL = $SQL . " WHERE id = $id2 ";
		
    $ret = mysql_db_query($db,$SQL,$cid);
		
    $row = mysql_fetch_array($ret);
        $artist = $row["artist"];
		$title = $row["title"];
		$id3 = $row["id"];
		

?>

<form name="form1" method="post" action="requestcd.php"> 
<INPUT TYPE="hidden" NAME="id" VALUE="<? echo("$id3"); ?>">

<input name="name" type="text" id="name">

<input type="submit" name="Submit" value="Submit">

</form>

When I post the form I get the following message, "Notice: Undefined index: id in ...requestcd.php on line 61", which refers to the following line.

Code:
if(isset($_POST['submit']))  {$id2 = $_POST['id'];} else {$id2 = $_GET['id'];}
 
Well, clearly then id is not found either in $_GET or $_POST array. Does this happen on the first loop? Are you sure the id is passed in the address line?
 
The page loads fine when its requested, no errors at all, i have also now added an echo to show the id that it is getting and it is the same as the one that is passed in the url.

Its only when I submit the form on that page that I get the error. I think that the problem lies in the hidden input not passing the id when it is submitted.
 
Are you sure submit is being sent? Certain browsers (IE) do not send the submit button informatioon if the form is sent via enter button. In that case, your script would be looking for id in the $_GET and fail due to that.

I suggest you output the $_POST array and see what is there:
Code:
echo '<pre>';
print_r ($_POST);
echo '</pre>';
 
I have added your code in to the page and the results are as follows.

Code:
Array
(
    [id] => 1
    [name] => 
    [Submit] => Submit
)

This has now confused me even more!!
 
Code:
if(isset($_POST['submit']))
You're checking for [tt]submit[/tt] while what you have there is [tt]Submit[/tt]. Variable names in php are case sensitive. Change that and you should be fine.
 
Thanks very much. That solved it.

I cant believe i over looked something as simple as that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top