Page 1 of 1

php conditions

Posted: Mon Dec 20, 2010 12:51 pm
by SamE4u
I am a noob and have learned everything I know through the internet. This is the one thing I couldn't find so far. I am trying to write a code for my test-site that checks the fields to make sure they aren't empty before submitting the query. I found some things, but it is all in javascript. Is there a way to do this without using javascript, just php or html? Not that I have anything against the language, but I just don't want to have to learn a whole new third language to write one page.
Here is my code so far, and thanks in advance:

<html>
<head>
<title>Insert</title>
</head>
<!--What is inserted will be posted and the person
will be forwarded to sql.php"-->
<form method="post" action="sql.php">
<center>
<p><strong> Your name?</strong> <input type="text" name="Name"/> </p>
<p><strong> Your age?</strong> <input type="text" name="Age"/> </p>
<p><strong> Your height?</strong> <input type="text" name="Height"/> </p>
<p><input type="submit" value="Insert"/> </p>
</center>
</form>
</html>
<center>

Code: Select all

<?php
//Have to make it so all fields must be complete in order to submit.
 $Name = $_POST['Name'];
 $Age = (int) $_POST['Age'];
 $Height = (double) $_POST['Height'];
 $pos = strpos($Name, $Age, $Height);
 if ($pos === false)
  echo "Please fill the fields";
?>
</center>
</html>

Re: php conditions

Posted: Mon Dec 20, 2010 1:45 pm
by requinix

Code: Select all

$pos = strpos($Name, $Age, $Height);
:|

strpos

Code: Select all

<?php
//Have to make it so all fields must be complete in order to submit.
if (empty($_POST['Name']) || empty($_POST['Age']) || empty($_POST['Height'])) {
  echo "Please fill the fields";
} else {
  $Name = $_POST['Name'];
  $Age = (int) $_POST['Age'];
  $Height = (double) $_POST['Height'];
  // ...
}
?>
empty

Re: php conditions

Posted: Mon Dec 20, 2010 2:19 pm
by SamE4u
Well that was really simple.
Don't mean to waste your time.
But, when I submit, the page still takes the 'form' action which is sql.php. I want it so that when the fields are empty, no action is taken and the message pops up.

Re: php conditions

Posted: Mon Dec 20, 2010 7:41 pm
by requinix
PHP can't interact with the browser. The closest you can get is to use JavaScript to act as an intermediary.

Re: php conditions

Posted: Tue Dec 21, 2010 1:39 pm
by SamE4u
Great.
Well, thank you. Do you have any websites or google search terms that would key me in the right direction?

Re: php conditions

Posted: Tue Dec 21, 2010 1:58 pm
by Jonah Bron
Javascript form validation.

Re: php conditions

Posted: Tue Dec 21, 2010 3:50 pm
by SamE4u
Perfect.
found it and it works.
Guess I'll do some study with java....

Re: php conditions

Posted: Tue Dec 21, 2010 3:59 pm
by Jonah Bron
NO NO NO NO NO!!!!!!!!!!!!

I said Javascript, not Java. Two COMPLETELY different things. :)

Re: php conditions

Posted: Tue Dec 21, 2010 4:30 pm
by kristen
SamE4u wrote:Well that was really simple.
Don't mean to waste your time.
But, when I submit, the page still takes the 'form' action which is sql.php. I want it so that when the fields are empty, no action is taken and the message pops up.
Remember to have validation on the backend as well. Javascript can easily be bypassed. You probably want to read up on "form spoofing" as well.