Closed Thread Icon

Topic awaiting preservation: PHP help please... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12237" title="Pages that link to Topic awaiting preservation: PHP help please... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP help please... <span class="small">(Page 1 of 1)</span>\

 
YoYoFREAK
Paranoid (IV) Mad Scientist

From: A lost remnant
Insane since: Jun 2001

posted posted 05-28-2002 06:53

I'm trying to get a PHP script to write to a database and check whether or not a form element has been filled out, my problem is that if the form has not been filled out it will write a row to the database anyway without any information. I don't know how to write a statement that would say if the element has been filled out then write to the database once the script has the information. Any Help would be much appreciated.
Thanks!
-YoYoFREAK

P.s sorry if I sound like an *idiot*, just trying to learn hehe.

The script used to connect to the database:

include("db_connect.php");

$query = "insert into orders(name) values('$name')";

mysql_query($query) or
die(mysql_error());


The script:

<?php

function validate_form()
{
global $name;

$errors=0;
if (!trim($name))
{echo "<br>A <b>First Name</b> is required.";
$errors++;
}
switch ($errors)
{
case 0:
include("db_connect.php");

$query = "insert into orders(name) values('$name')";

mysql_query($query) or
die(mysql_error());
return TRUE;
break;

case 1:
echo "<br /><br /> Please use your browsers back button to correct the error and re-submit the order.";
return FALSE;
break;

default:
echo "<br /><br /> Please use your browsers back button to correct the error and re-submit the order.";
return FALSE;

}
}

function thank_you()
{

echo "Thank you your order has now been Processed";


}

$ok = validate_form();
if ($ok)
thank_you();


?>

(Edit: Updated code)


[This message has been edited by YoYoFREAK (edited 05-28-2002).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-28-2002 08:04

Your switch statement is wrong. The correct syntax is:

switch ($errors)
{
case 0:
return TRUE;
break;
case 1:
echo "<br /><br /> Please use your browsers back button to correct the error and re-submit the order.";
return FALSE;
break;
default:
echo "<br /><br /> Please use your browsers back button to correct the error and re-submit the order.";
return FALSE;
}

BTW Since $errors in your validate_form() function can only be 0 or 1, I would use an if statement instead of switch...




[This message has been edited by mr.maX (edited 05-28-2002).]

YoYoFREAK
Paranoid (IV) Mad Scientist

From: A lost remnant
Insane since: Jun 2001

posted posted 05-28-2002 19:48

Thanks Max! Your tips helped out very much. But for some reason I couldn't get the if statement to work in place of the switch statement without getting a parse error.

But I have another question, sorry . When I check form elements to see if information has been added, sometimes I use 3 text boxes for one item such as a phone number each text box having a different name, but how would I group all three form elements as one element so that if one text box is missing all that is displayed is "A phone number is required" instead of checking for each individual text element and saying "a first prefix for the phone number is required, a second prefix...."

I tried using this code.....


<?php

function validate_form()
{
$phone = "$phoneone, $phonetwo, $phonethree";

global $phone;

$errors=0;
if (!trim($phone))
{echo "<br>A <b>Phone Number</b> is required.";
$errors++;
}

}
switch ($errors)
{
case 0:
include("db_connect.php");

$query = "insert into orders(phone) values('$phone')";

mysql_query($query) or
die(mysql_error());
return TRUE;
break;

case 1:
echo "<br /><br /> Please use your browsers back button to correct the error and re-submit the order.";
return FALSE;
break;

default:
echo "<br /><br /> Please use your browsers back button to correct the error and re-submit the order.";
return FALSE;

}

}

function thank_you()
{

echo "Thank you your order has now been Processed";

}


$ok = thank_you();

if ($ok)
thank_you();
?>

But the problem with it is that, the error will be displayed saying "A phone number is required" but also displays the Thank You message and writes to the database, does anyone know how to fix this? I would much appreciate any help.
Thanks!
-YoYoFREAK



[This message has been edited by YoYoFREAK (edited 05-28-2002).]

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 05-28-2002 20:39

Is all your code here? Because it seems like stuff is missing. For one thing, I don't see the update_database() function declared anywhere.

Actually the whole second version you posted is completely screwed up. Perhaps you made a pasting error?

Anyway, there are three problems here that I see. First you set the local variable $phone then you declare $phone global after that. You need to make $phone global first, otherwise it changes it's reference to an empty variable after you have set it as a local variable. Make sense? But that won't solve your problem, because you are building $phone from three global variables, except you haven't declared them global, so $phone will always be empty. Then the third problem is that $phone won't actually be empty, because you are building it with commas, so !trim($phone) will ALWAYS be true. So declare $phoneone, $phonetwo, and $phonethree to be global, then you could make a comparison like (!phoneone &#0124; &#0124; !phonetwo &#0124; &#0124; !phonethree). Of course, that doesn't even verify that there are numbers in the fields. People could enter anything they want and it would get put in the database.

-jiblet

YoYoFREAK
Paranoid (IV) Mad Scientist

From: A lost remnant
Insane since: Jun 2001

posted posted 05-29-2002 10:25

Jiblet,
I'm sorry I am just not quite getting it, I tried makeing $phoneone $phonetwo and $phonethree global and then using:

$errors=0;
if (!trim($phoneone)&&!trim($phonetwo)&&!trim($phonethree))
{echo "<br>A <b>Phone Number</b> is required.";
$errors++;
}

but that didn't work either =(. Am I missing something or just way off the target?
Thanks very much Jiblet for explaining, I just have a hard time getting it hehe.
-YoYoFREAK

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-29-2002 19:07

Alright this is where a class is called for. Here I whipped this up

code:
<? 
class validate_form {
var $numbers;
var $ERROR;

function v() {
if (!$this->numbers[0] &#0124; &#0124; !$this->numbers[1] &#0124; &#0124; !$this->numbers[2]) {
$this->ERROR = "A <b>Phone Number</b> is required.";
return false;
} else {
return true;
}

}

function updatedb() {
include("db_connect.php");
$this->ERROR = false;
foreach ($this->numbers as $var) {
$query = "insert into orders(phone) values('$phone')";
$q = mysql_query($query);
if (!$q) {
$this->ERROR .= mysql_error()."<br>";

}

}
if ($this->ERROR) {
return false;
} else {
return true;
}

}

function s_num() {
if (!$this->v()) {
return false;
} elseif (!$this->updatedb()) {
return false;
} else {
return true;
}
}
}



Here's how you use it:
// Set up some sample variables you wouldn't need these
$phone1 = 5551212;
$phone2 = 5551212;
$phone3 = 5551212;

// Create an instance of the validate_form class;
$fv = new validate_form();

// Stick all the phone numbers in an array that is a member of
// this class for ease of use (it lets me loop);
$fv->numbers = array($phone1, $phone2, $phone3);

// Run the one function that will call the validate and updatedb functions
// Since these are all members of the same class I don't need to worry
// about scoping variables.
// NOTE -- personally I don't like echoing HTML in functions or classes so notice
// while my class has that ERROR variable that I can grab all echo's are outside
// the class
if (!$fv->s_num()) {
echo "The following errors were encountered:<br>";
echo $fv->ERROR."<br>";
echo "Please use your browsers back button to correct the error and re-submit the order";
} else {
echo "Thank you for submitting";
}
?>

The other nice thing about classes is that you can just put the class code into an included library and either use it anywhere and clean up the code.





.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 05-29-2002 19:57

All I can say about that code snippet is that it will only work if $phoneone, $phonetwo, and $phonethree are all empty. Of course, it's probably an invalid phone number if ANY of the fields are missing, which is why I suggested using &#0124; &#0124; rather than &&.

If it's still not working I need more information to solve the problem. The last code you posted was hopefully not an exact copy because it had many big structural problems that the first copy of the code didn't. Perhaps you could put a .txt suffix at the end of your script and post a link to it so I can examine the entire code.

Oh yeah, bitdamaged's solution is more ergonomic, but I sense you are trying to learn here, so that might defeat the purpose.

-jiblet

Dark Phoenix
Paranoid (IV) Inmate

From: Harrow, Ontario, Canada
Insane since: Feb 2002

posted posted 05-30-2002 20:11

When I need to check a form for errors, I use an array called $errors, and use a bunch of if statements. Like this:

if(!isset($username))
{
$errors[] = "You must enter a username!";
}
if(!isset($password))
{
$errors[] = "You must enter a password!";
}

Then just use this:

if(isset($errors))
{
//Insert error code here
}else{
//Insert standard code here
}

I don't know if that'll help, but it's worth a shot.

"No one's going to give you a map; you've got to walk your own path." - Hot Ice Hilda, Outlaw Star.

YoYoFREAK
Paranoid (IV) Mad Scientist

From: A lost remnant
Insane since: Jun 2001

posted posted 06-04-2002 03:14

I just wanted to say thanks for all of the help. (I know its a bit late) but I wanted to take a break from the computer for awhile. I will probably study the scripts posted and see if I can figure out how they work a bit later on once I figure out how to make this stupid If statement work *arg* lol. Thanks for all of the help though, I much appreciate it.

« BackwardsOnwards »

Show Forum Drop Down Menu