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!

undefined variable on using form on post method

Status
Not open for further replies.

codo

IS-IT--Management
Oct 31, 2001
187
ID
i'm a newbie on PHP. this is my two sample php file :
1st :<form2php1.php>
<html>
<head> <title>Pengelolaan Data Toko buku Serba Murah</title></head>

<body>
<center>
<h2>Pengelolaan Toko Buku Serba Murah</h2>
<h3>INPUT DATA BUKU</H3>
</center>

<hr>
<form action=form2php2.php method=post>

<b>Judul Buku :</b>
<input type=text name=judul><br>
<b>Sekilas isi :</b>
<textarea name=sekilas></textarea><br>
<b>Jenis Buku :</b>
<input type=text name=jenis><br>
<b>Nama File Gambar :</b>
<input type=text name=nama_gmb><br>
<b>Nama Penerbit :</b>
<input type=text name=penerbit><br>
<b>Nama Penulis :</b>
<input type=text name=penulis><br>
<b>Harga Buku :</b>
<input type=text name=harga><br>
<b>Banyaknya :</b>
<input type=text name=jumlah><br>
<input type=submit value=PROSES>
<input type=reset value=ULANGI>
</form>
<hr>
</body>
</html>


the second :<form2php2.php>
<html>
<head> <title>Pengelolaan Data Toko buku Serba Murah</title></head>

<body>
<center>
<h2>Pengelolaan Toko Buku Serba Murah</h2>
<h3>DATA BUKU</H3>
</center>
<hr>

<?php
// menampilkan data yang dikirimkan dari file form2php1.php

echo"
<b>Judul Buku :</b> $judul<br>
<b>Sekilas isi :</b> $sekilas<br>
<b>Jenis Buku :</b> $jenis<br>
<b>Nama File Gambar :</b> $nama_gmb<br>
<b>Nama Penerbit :</b> $penerbit<br>
<b>Nama Penulis :</b> $penulis<br>
<b>Harga Buku :</b> $harga<br>
<b>Banyaknya :</b> $jumlah<br>";
?>

<hr>
</body>
</html>


when i run the first file <form2php1.php> and fill the form and then process it to next file i got error message like this :
Notice: Undefined variable: judul in c:\Inetpub\ on line 15

Notice: Undefined variable: sekilas in c:\Inetpub\ on line 16

Notice: Undefined variable: jenis in c:\Inetpub\ on line 17

Notice: Undefined variable: nama_gmb in c:\Inetpub\ on line 18

Notice: Undefined variable: penerbit in c:\Inetpub\ on line 19

Notice: Undefined variable: penulis in c:\Inetpub\ on line 20

Notice: Undefined variable: harga in c:\Inetpub\ on line 21

Notice: Undefined variable: jumlah in c:\Inetpub\ on line 22

can somebody tell me what's wrong with these script?
 
You are calling variables which have not been defined. That is because you are assuming register_globals on. That feature has been turned off for security reasons in newer versions of php. You need to reference your form sent variables via one of the superglobals ($_GET or $_POST). In your case that would be $_POST. For instance, instead of $jumlah you would have $_POST['jumlah']. Also, make sure you do not reference these variables within double quotes, because PHP does not like that. Rather concatenate the values.

More here:
 

Thank's to Vragabond for the solution. But there's something confusing me. this is the example that i had made. The first echo command was succesfully display the result of Title Variable, but the second echo command i got this error message :
"PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING ..... "


this is the script :
<html>
<head> <title>Book Store</title></head>
<body>
<?php
// first echo command that worked
echo "<b>Book title :</b> ";
echo $_POST['title'];

// 2nd echo command that not worked
echo "<b>Book title :</b> $_POST['title'] ";
?>
</body>
</html>

does the $_POST['VARIABLE'] command have to be on a single line and can be combined with other text/string ?
like this line give me error message :
$query = "INSERT INTO book (title)";
$query .= "values ($_POST['title'])";

but this one is ok :
$query = "INSERT INTO book (title)";
$query .= "values (";
$query .= $_POST['title'];
$query .= ")";
 
You nearly answered your question yourself. PHP does not like arrays with string keys inside the double quotes. That is why you have to escape the quotes and concate the values. You can do it like you did, or even in one line:
Code:
 $query   = "INSERT INTO book (title) values ('" . $_POST['title'] . "')";
 
Thanks Vragabond for the concate explanation. I also tried like this :
$query = "INSERT INTO book (title)";
$query .= "values ('{$_POST['title']}')";
and it works.

I also have another case on sending a variable. the 1st script goes like this :
....
<a href='category.php?mtype=$line[0]'> $line[0]</a>;
....

on the category.php script i tried to display a query based on the value of mtype variable:
...
$query ="SELECT * FROM book WHERE book_type='$jenis' ";
...

and the result goes like this :
PHP Notice: Undefined variable: mtype in c:\Inetpub\ on line 19

i already tried using
$query ="SELECT * FROM book WHERE book_type='" .$jenis ."'";
and the result still the same.
 
Just like the errors say, $jenis is not a defined variable. Check your script. Where does $jenis come from? Is it possible you mistyped it? Or is it coming from a form or session and is as it is now relying on register globals to be on. The error tells you that it is not the problem in the query but rather telling you that you are comparing book_type to something that is nowhere defined as anything (which is also why query is not returning anything).
 
ups i mean on the category.php script are like this :
...
$query ="SELECT * FROM book WHERE book_type='$mtype' ";
...

and the result goes like this :
PHP Notice: Undefined variable: mtype in c:\Inetpub\ on line 19

i already tried using
$query ="SELECT * FROM book WHERE book_type='" .$mtype ."'";
and the result still the same.

It seem that the problem was about how to write a passing variable from one script to another sript.
 
My answer still holds. What is in the $mtype variable? What are you expecting it is? Clearly, PHP says that the variable is not defined and it is usually right. If the variable is coming from another page, how was it passed: post, get, session? If it is from the same page, is it being using outside its scope -- variables defined outside functions will not be available in the functions without being declared global? Help us help you. Tell us where $mtype is coming from and what do you expect it to contain.
 
Well Finaly i've the answer for my own question :)
I found it from where it give explanation on how to collecting data via HTML Forms. The right script for read a variable via html form are using $_GET['variable'] method, so i changed my sript like this :
...
$query ="SELECT * FROM book WHERE book_type='" .$_GET['mtype']. "'";
...
and everything was OK now. Thanks
 
codo said:
The right script for read a variable via html form are using $_GET['variable'] method
This is true for your case but saying what you just said is misinformation. HTML forms have two distinct methods for sending data across the pages: get and post. This method is specified in the html form tag and will, when omitted, defaults to get. Based on which method is used, PHP will put sent variables in one of the corresponding superglobal arrays: $_POST for post method and $_GET for get method. Variables from post and get methods are both available in another superglobal array called $_REQUEST, however the usage of that array raises similar security issues as using register_globals on.

From what you wrote, we had no idea where your undefined variable is coming from. And as such we did not have enough accurate information to help you with the problem. It is just as top expert here says: Want the best answers? Ask the best questions.
 
the $mtype variable came from other page(for example 'index.php') that i wrote like this as a link:

<html>
<body>
<h3>Choose Book Category</h3>
<ul>
<?php
include("database.php");
$query = "SELECT DISTINCT type From book";
$result_query = mysql_query($query, $conection);

while ($line = mysql_fetch_row($result_query))
{
echo"<li>
<a href='category.php?mtype=$line[0]'>
$line[0]</a>";
}
?>
</ul>
</body>
</html>


and the 'category.php' goes like this:

<html>
<body>
<?php
include("database.php");
echo"<table border=1>";
$query ="SELECT * FROM booj WHERE type='" . $_GET['mtype']. "'";

$result_mysql=mysql_query($query, $conection);
while($line=mysql_fetch_row($result_mysql))
{
echo"<tr>
<td><img src='./image/$line[2]' height=150></td>";
echo"<td><b>$line[1]</b></td>
</tr>";
}
echo"</table>";
?>
</body>
</html>

I hope it will clear my question. Sorry for my bad English and thanks for your help. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top