php conditions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SamE4u
Forum Newbie
Posts: 4
Joined: Mon Dec 20, 2010 12:44 pm

php conditions

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php conditions

Post 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
SamE4u
Forum Newbie
Posts: 4
Joined: Mon Dec 20, 2010 12:44 pm

Re: php conditions

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php conditions

Post by requinix »

PHP can't interact with the browser. The closest you can get is to use JavaScript to act as an intermediary.
SamE4u
Forum Newbie
Posts: 4
Joined: Mon Dec 20, 2010 12:44 pm

Re: php conditions

Post by SamE4u »

Great.
Well, thank you. Do you have any websites or google search terms that would key me in the right direction?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php conditions

Post by Jonah Bron »

Javascript form validation.
SamE4u
Forum Newbie
Posts: 4
Joined: Mon Dec 20, 2010 12:44 pm

Re: php conditions

Post by SamE4u »

Perfect.
found it and it works.
Guess I'll do some study with java....
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php conditions

Post by Jonah Bron »

NO NO NO NO NO!!!!!!!!!!!!

I said Javascript, not Java. Two COMPLETELY different things. :)
kristen
Forum Newbie
Posts: 14
Joined: Tue Sep 07, 2010 5:51 pm

Re: php conditions

Post 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.
Post Reply