PDA

View Full Version : script problem


0810
10-03-2002, 08:59 PM
Hi my script is something worng.

I got error anytime whenever I test it.

my php is 4.22

my php editor says




Notice: Undefined index: comments in c:itoguestbook.php on line 15




here is my script





<?php
if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"){
$con = mysql_connect("localhost","itohideo","1234");
if(!$con) {
print("Fail DB Connection<br>\n");
exit;
}
if(!mysql_select_db("itohideo")) {
print("Fail DB Selection<br>\n");
exit;
}
$name = $_POST["name"];
$location = $_POST["location"];
$url = $_POST["url"];
$comments=$_POST["comments"];
$query_insert = "insert into guestbook (name, location, url, comments) values ('$name', '$location', '$url','$comments')";
$query_result_insert = mysql_query($query_insert, $con) or die (mysql_error($con));
Print("thank you for filling out<a href=\"newyork.php\">Main Page</a><br>\n");
mysql_close($con);
} else {
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<P>please fill out this form and it goes to my database</P>
Full Name<input name="name" type="text"><br>
Location(where you live)<input name="location" type="text"><br>
Home Page(URL)<input name="url" type="text"><br>
Comments<textarea cols="40" rows="10" name="comments" ></textarea>
<input type="submit" name="submit">
<input type="reset" name="reset">
<input type="hidden" name="formSubmitted" value="yes">
</form>
<?}?>

mordred
10-03-2002, 09:45 PM
The code seems ok to me... are you sure you reloaded the page after changing it the last time (I know, that sounds like a really stupid question, but such things happen)?

0810
10-04-2002, 05:27 AM
what I am trying to do is if a user doesn't fill out name, it comes warning like "you miss filling out "name"".



How can I do it?? I have to use "alert" right. I think that I have to use "Javascirpt" to do it but I don't how to use with php.
could you explain with my script. Please help

Thank you very much

I did like this







<?php
if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"){
$con = mysql_connect("localhost","itohideo","1234");
if(!$con) {
print("Fail DB Connection<br>\n");
exit;
}
if(!mysql_select_db("itohideo")) {
print("Fail DB Selection<br>\n");
exit;
}
$name = $_POST["name"];
$location = $_POST["location"];
$url = $_POST["url"];
$comments = $_POST["comments"];
if(empty($name))
{

print("you have to put at least one word<br>\n");

}

if(empty($location)){

print("missing location please put your location<br>\n");


}

if(empty($url)){

print("Do you have any url<br>\n");


}

if(empty($comments))
{
print("come on even one word");

}




$query_insert = "insert into guestbook (name, location, url, comments) values ('$name', '$location', '$url','$comments')";
$query_result_insert = mysql_query($query_insert, $con) or die (mysql_error($con));
print("Thank you for filling out<br>\n");
mysql_close($con);
} else {
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<P>please fill out this form and it goes to my database</P>
Full Name<input name="name" type="text"><br>
Location(where you live)<input name="location" type="text"><br>
Home Page(URL)<input name="url" type="text"><br>
Comments<textarea name="comments" cols="40" rows="10"></textarea><br>
<input type="submit" name="submit">
<input type="reset" name="reset">
<input type="hidden" name="formSubmitted" value="yes">
</form>
<?}?>

Nightfire
10-04-2002, 12:50 PM
if (!$url){
echo 'Enter your url';
}
if(!$name){
echo 'Enter your name';
}

etc

usban
10-04-2002, 02:07 PM
You have to do something like that:

if(empty($name))
{
header("location: contes.php?error=1");
exit();
}

if(empty($location)){
header("location:contes.php?error=2");
exit();
}
AS MUCH AS YOU WANT

And then inseide the else clause bau before the form:

<? if(isset($error)) { ?>
<script>
function alerta(error)
{
switch(error) {
case "1":
alert("you have to put at least one word");
break;
case "2":
alert("missing location please put your location");
break;

AS MUCH AS YOU WANT
}
}
</script>
<? } ?>
</head>
<body<? if(isset($error)) echo " onLoad=alerta('$error')" ?>>

0810
10-04-2002, 07:28 PM
usban I did your way but still don't work . I have a QUA. I should make contes.php. Here



header("location: contes.php?error=1");



if so, what I should wirte for in it???????

Thanks for helping urban

Here is new code















<?php
if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"){
$con = mysql_connect("localhost","itohideo","1234");
if(!$con) {
print("Fail DB Connection<br>\n");
exit;
}
if(!mysql_select_db("itohideo")) {
print("Fail DB Selection<br>\n");
exit;
}
$name = $_POST["name"];
$location = $_POST["location"];
$url = $_POST["url"];
$comments = $_POST["comments"];

if(empty($name))
{
header("location: contes.php?error=1");
exit();
}

if(empty($location)){
header("location:contes.php?error=2");
exit();
}


if(empty($url))
{
header("location: contes.php?error=1");
exit();
}

if(empty($comments)){
header("location:contes.php?error=2");
exit();
}



else




<? if(isset($error)) { ?>
<script>
function alerta(error)
{
switch(error) {
case "1":
alert("you have to put at least one word");
break;
case "2":
alert("missing location please put your location");
break;
case "3":
alert("missing url please put your url");
break;
case "4":
alert("missing your comments");
break;
}
}
</script>
<? } ?>
</head>
<body<? if(isset($error)) echo " onLoad=alerta('$error')" ?>>



$query_insert = "insert into guestbook (name, location, url, comments) values ('$name', '$location', '$url','$comments')";
$query_result_insert = mysql_query($query_insert, $con) or die (mysql_error($con));
print("Thank you for filling out my form<a href=\"ja1test.php\">Go back to main page</a><br>\n");
mysql_close($con);
} else {
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<P>please fill out this form and it goes to my database</P>
Full Name<input name="name" type="text"><br>
Location(where you live)<input name="location" type="text"><br>
Home Page(URL)<input name="url" type="text"><br>
Comments<textarea name="comments" cols="40" rows="10"></textarea><br>
<input type="submit" name="submit">
<input type="reset" name="reset">
<input type="hidden" name="formSubmitted" value="yes">
</form>
<?}?>

0810
10-05-2002, 02:35 AM
Please help my script!

thank you very much

0810
10-05-2002, 09:21 PM
urban please help me my script it is not working

thank you very much

any help????????

usban
10-05-2002, 11:26 PM
I forgot to remove the name of the file in which i make some tests, contes is the name i gave to the file instead of it you should wirte the name of your file.

What the header("location: file.php") does is to redirect the page to the target file is sais here, also you can pass some parameters . In this example you pass the variable error.

I think with this it should work but let me know if you have any problem

:thumbsup:

0810
10-06-2002, 04:27 AM
urban thanks

but it is not working yet. So could you explain with my scirpt then I will understand.

I actually made a file contes.php.

header("Location:contes.php like this

but what should I write in contes.php???????I just made"contes.php" which is empty. I think that my file(contes.php) comes out "you have to put at least one word" if I don't input "name"

I have no idea??

again urban could you explain for me with my script??

thank you very much

usban
10-06-2002, 11:26 AM
I've tried this code and it works. In all the header("location: ...") i put contest.php, this is the name of the file in which i've tried thius code, instead of "contes.php" you have to place the name of the file in which you are going to place this code.
If there is something you don't understand aks me. :thumbsup:

<?php
if(isset($Submit))
{
$con = mysql_connect("localhost","itohideo","1234");
if(!$con)
{ print("Fail DB Connection<br>\n");
exit; }

if(!mysql_select_db("itohideo"))
{ print("Fail DB Selection<br>\n");
exit; }

if(empty($name))
{ header("location: contes.php?error=1");
exit(); }

if(empty($location))
{ header("location:contes.php?error=2");
exit(); }

if(empty($url))
{ header("location: contes.php?error=3");
exit(); }

if(empty($comments))
{ header("location:contes.php?error=4");
exit(); }

$query_insert = "insert into guestbook (name, location, url, comments) values ('$name', '$location', '$url','$comments')";
$query_result_insert = mysql_query($query_insert, $con) or die (mysql_error($con));
print("Thank you for filling out<br>\n");
mysql_close($con);


} else {
if(isset($error)) { ?>
<script>
function alerta(error)
{
switch(error) {
case "1":
alert("you have to put at least one word");
break;
case "2":
alert("missing location please put your location");
break;
case "3":
alert("missing url please put your url");
break;
case "4":
alert("missing your comments");
break;
}
}
</script>
<? } ?>
</head>
<body<? if(isset($error)) echo " onLoad=alerta('$error')" ?>>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<P>please fill out this form and it goes to my database</P>
Full Name<input name="name" type="text"><br>
Location(where you live)<input name="location" type="text"><br>
Home Page(URL)<input name="url" type="text"><br>
Comments<textarea name="comments" cols="40" rows="10"></textarea><br>
<input type="submit" name="Submit">
<input type="reset" name="reset">
</form>
<?}?>

mordred
10-06-2002, 03:45 PM
Small notice:

The header "location" is defined to expect an absolute URI as an argument. There are though clients that can deal with relative URIs, but not all. I don't know if this is part of the problem, but anyway, I said it now.

http://us.php.net/manual/en/function.header.php

0810
10-06-2002, 09:37 PM
Hi urban thanks

but it is not working yet

I confused that I have a file which is contes.php

like

<body>
<a href="guestbook.php">Go back go guestbook</a>
<body>

This is contes.php

and I saved my script as "guestbook.php"

Moreover, I did your way to run the script. However, not works

when I click submit, information is gone like reset. it doesn't go to next page or alert.

strange

could you help me again urban sorry to ask you

thank you for your time

usban
10-12-2002, 11:33 AM
Again i've tested it and it works, the structure is quite simple, first you see if the Submit variable is set, this means that the form has been previously send. Then you see that all the fields have been filled and if so, you connect to the database and make the query.

If it is the first time you arrive to the page you display the form. Now i've the name of the file "guestbook.php" because this is the name you use.


<?php
if(isset($Submit))
{

if(empty($name))
{ header("location: guestbook.php?error=1");
exit(); }

if(empty($location))
{ header("location:guestbook.php?error=2");
exit(); }

if(empty($url))
{ header("location: guestbook.php?error=3");
exit(); }

if(empty($comments))
{ header("location:guestbook.php?error=4");
exit(); }

$con = mysql_connect("localhost","itohideo","1234");
if(!$con)
{ print("Fail DB Connection<br>\n");
exit; }

if(!mysql_select_db("itohideo"))
{ print("Fail DB Selection<br>\n");
exit; }

$query_insert = "insert into guestbook (name, location, url, comments) values ('$name', '$location', '$url','$comments')";
$query_result_insert = mysql_query($query_insert, $con) or die (mysql_error($con));
print("Thank you for filling out<br>\n");
mysql_close($con);

}
else
{
if(isset($error)) { ?>
<html>
<head>
<script>
function alerta(error)
{
switch(error) {
case "1":
alert("you have to put at least one word");
break;
case "2":
alert("missing location please put your location");
break;
case "3":
alert("missing url please put your url");
break;
case "4":
alert("missing your comments");
break;
}
}
</script>
<? } ?>
</head>
<body<? if(isset($error)) echo " onLoad=alerta('$error')" ?>>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<P>please fill out this form and it goes to my database</P>
Full Name<input name="name" type="text"><br>
Location(where you live)<input name="location" type="text"><br>
Home Page(URL)<input name="url" type="text"><br>
Comments<textarea name="comments" cols="40" rows="10"></textarea><br>
<input type="submit" name="Submit">
<input type="reset" name="reset">
</form>
<?}?>

Sorry but i've been very busy the last week ;)