Page 1 of 1

Logic Issues

Posted: Sat Jun 30, 2007 1:37 pm
by byteyou
Hi Everyone,

Suppose in a situation that i need to validate a form using expression, my checking is done is a method say

checkName();
In this function, if the information entered is incorrect, i will print out "wrong input". this method together with the php and sql adding codes are place together in the same file as the form.

so whenever i wanna use this form, the moment i get into that page, the field is of course empty but the php code will think that there is an error and it prints the error msg as mentioned above. how do i set it to only run the php codes only when i hit the "SUBMIT" button and not run the code straight after i enter that page.

Thank you

Posted: Sat Jun 30, 2007 1:40 pm
by miro_igov

Code: Select all

if(isset($_REQUEST['name_of_the_submit_button'])) checkName();

Posted: Sat Jun 30, 2007 1:51 pm
by feyd
Look for something other than the submit button, if possible.

Re: Logic Issues

Posted: Sat Jun 30, 2007 8:21 pm
by califdon
byteyou wrote:Hi Everyone,

Suppose in a situation that i need to validate a form using expression, my checking is done is a method say

checkName();
In this function, if the information entered is incorrect, i will print out "wrong input". this method together with the php and sql adding codes are place together in the same file as the form.

so whenever i wanna use this form, the moment i get into that page, the field is of course empty but the php code will think that there is an error and it prints the error msg as mentioned above. how do i set it to only run the php codes only when i hit the "SUBMIT" button and not run the code straight after i enter that page.

Thank you
Not clear on how your script is structured. Presumably you have a switch statement that executes different blocks of code depending on whether it is being called with data from the form filled in. Under that assumption, you only call the checkName() function in the part of the code that executes when there is form data present.

Posted: Sun Jul 01, 2007 2:20 am
by byteyou
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


* this is only the partial code

Firstly is the html, i have the user name field and password field to be filled in and only submit that data when user clicks submit. after that i have the valiation function. the problem is the moment i load the page it runs all the php code which result in empty field and error. is there anyway to run the code only after i click submit and not straight away?

Regards

[syntax="html"]<!-- page header here -->
</td></tr><tr><td width='150' align='left' valign='top'>
<!-- side menu start -->
<a href="general.php">Home</a>
<!-- side menu end -->
</td><td align='left' valign='top'><blockquote>
<!-- right body start -->
  <form name="registerForm" method="post" onSubmit="return test()" action="register.php" id="registerForm"><br /><br />
  <table width="405" border="0">
  <tr>
    <td width="136">User Name</td>
    <td width="242"><input name="userName" type="text" size="20" maxlength="20" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input name="password" type="password" size="22" maxlength="10" /></td>
  </tr>
<input name="submit" type="submit" id="test" value="Register" />&nbsp;<input name="reset" type="reset" value="Clear" />
  </form>
/***************************************************************/
PHP
/**************************************/[/syntax]

Code: Select all

<?php 
include("inc/userAccValidation.inc");
include("inc/dbconnect.php");

$username = $_REQUEST["userName"]; 
$password = $_REQUEST["password"];
$cfmPassword = $_REQUEST["cfmPassword"];
$emailAdd = $_REQUEST["emailAdd"];
$name = $_REQUEST["name"];
$day = $_REQUEST["day"];
$month = $_REQUEST["month"];
$year = $_REQUEST["year"];
$date = $year."-".$month."-".$day;
$nationality = $_REQUEST["nationality"];
$gender = $_REQUEST["gender"];
$tertiary = $_REQUEST["tertiary"];
$course = $_REQUEST["course"];
//if ($username!="" && $password!= "" && $cfmPassword!="" && $emailAdd!="" && $name!="") {

	if(validatePassword($password,$cfmPassword) && ValidateInput($username,$password,$emailAdd,$name)) {

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 01, 2007 3:36 am
by miro_igov
Remove the comment from

Code: Select all

//if ($username!="" && $password!= "" && $cfmPassword!="" && $emailAdd!="" && $name!="") {

Posted: Tue Jul 03, 2007 12:25 pm
by califdon
I don't see where you are detecting whether this execution of the script is for the first time, for validation, or subsequently, for database entry. If you're going to use the same script for both, you have to determine which part of the PHP logic you want to follow. I usually do that with the PHP switch syntax. For instance, you could use a hidden input control whose value you set in your code if the user has already been validated, then test for that in your switch statement.

Actually, why is it important for you to check validation before inputting the whole form? In fact, won't some users neglect to click the Submit button and do that anyway?

Another cool way to do it would be to use Ajax.