PHP If statment not working Please help

Get help on web editors (Frontpage, Dreamweaver) and web languages (HTML, ASP, PHP).
Post Reply
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

PHP If statment not working Please help

Post by SBSP »

Hi I'm trying to learn PHP but i'm about to blow a gasket!

Im trying to run an if statment.


Here is the code

Code: Select all

<?php

If($FirstLoad="0")
{Echo " [not First Load]  ";}
else
{echo " [First Load] ";}




$FirstLoad="0";
?>

It always prints

Code: Select all

 [not First Load]  
The reason i want to do this is when the page loads i dont want the PHP code to run it must only run after i pressed a button.
The stupid If statement doesnt work!

how do i make PHP code only run when i press a button. ?
Last edited by SBSP on 10 Oct 2008, 11:31, edited 1 time in total.
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Re: PHP If statment not working Please help

Post by Kronos »

You need to use 2 = signs in an if statement, otherwise you are assigning a value and it will always be true.

i.o.w.

Code: Select all

<?php

If($FirstLoad == "0")
{Echo " [not First Load]  ";}
else
{echo " [First Load] ";}




$FirstLoad="0";
?>
Image
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Re: PHP If statment not working Please help

Post by SBSP »

but == means is not equal to ?
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Re: PHP If statment not working Please help

Post by Kronos »

SBSP wrote:but == means is not equal to ?
no. not equal to is

Code: Select all

!=
Image
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Re: PHP If statment not working Please help

Post by SBSP »

Kronos wrote:
SBSP wrote:but == means is not equal to ?
no. not equal to is

Code: Select all

!=
Thanks i made a paralax fault on w3Schools website. He he :mrgreen:

But anyway it still doesnt work :evil:
I have another page with PHP code and that works.

Code: Select all

<?php

   $Username=$_POST["username"];
   $Password=$_POST["password"];
   $Encrypted="";
   
   

  if ($_POST["username"]=="myusername")
  echo "TRUE";
  else
  echo "Incorrect Username or Password";


?>
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Re: PHP If statment not working Please help

Post by SBSP »

But I think when i press the button it reruns the whole page from scratch.
resetting $Firstload 's value.


But even if it is that it still always prints Not First Load.
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Re: PHP If statment not working Please help

Post by Kronos »

SBSP wrote:But I think when i press the button it reruns the whole page from scratch.
resetting $Firstload 's value.


But even if it is that it still always prints Not First Load.
Yeah, this is a little tricky to get it working in PHP. I don't really know PHP, so don't take my word as authority though.

But you might need to check something like:

Code: Select all

if (!isset($_POST))
{
 //first load
}
I imagine you might be able to use your own variables for this, such as:

Code: Select all

<?php

If(!isset($FirstLoad))
{Echo " [not First Load]  ";}
else
{echo " [First Load] ";}




$FirstLoad="0";
?>
try and play around with that.
Image
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Re: PHP If statment not working Please help

Post by SBSP »

wil do thanks man
cbrunsdonza
Registered User
Posts: 5
Joined: 11 Oct 2008, 21:48

Re: PHP If statment not working Please help

Post by cbrunsdonza »

SBSP wrote:Hi I'm trying to learn PHP but i'm about to blow a gasket!

The reason i want to do this is when the page loads i dont want the PHP code to run it must only run after i pressed a button.
The stupid If statement doesnt work!

how do i make PHP code only run when i press a button. ?
Hi. I'm a PHP developer so I can shed a bit more light on your original issue.

PHP is server side not client side (and thats what you wanted to do!). When you request a PHP page from your WEB SERVER (IIS / Apache) it will first execute the PHP generating a output file. The output file is what you see in the web page, not the original PHP code. PHP is not a dynamic language that can be "paused" and then "resumed".

What you want is rather to have page 1 that generates the form. This in turn calls page 2 which process the form. You will find that you will have many small PHP files when your done. If your trying to avoide this, then you need to look into PHP frameworks but that will be beyond your current knowledge.

But I will give a shot at what your trying to achieve here:

Code: Select all

<?php

if (isset($_REQUEST['FirstLoad'])) // dose this param exist ... usualy in the URL ?
{
  $FirstLoad = $_REQUEST['FirstLoad']; // OK then allocate its value
}
else
{
  $FirstLoad = 0;  // or set to default value
}

IF($FirstLoad=="0") // in PHP you can also do: IF (!$FirstLoad)  or IF (is_null($FirstLoad)) or IF($FirstLoad==FALSE)
{
  echo " [not First Load]  ";
  // lets add in our form ...
  ?>
  <form method='post' ....>
     <input type='text' name='FirstLoad' value='1' ></input>
     ....
     <button type='submit'>
  </form>
  <?php
}
else
{
  echo " [First Load] ";
}
?>
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Re: PHP If statment not working Please help

Post by SBSP »

cbrunsdonza I really do appreciate your help!.

Thanks I'm going to give this a try.

I also have some questions regarding variables.

In php you dont have to declare them you just use them and on the first time of the variable being used it
registers , if you know what i trying to say.

Lets say i have 2 pages, index.php and in index.php i have a form with 2 text boxes 'txtUsername ,txtPassword' with username input type set to 'text and password input type set to 'password' and a submit button called 'Login'. Linking to another page called welcome.php.

in welcome.php iretrieve the values from the 2 text boxes on index.php

Code: Select all

$User_Username=$_POST["txtUsername"];
$User_Password=$_POST["txtPassword"];
$EncryptedPassword=crypt("$User_Password",736358903544);

I then connect to a MySql database using the root and blank password (Wamp defaults)
and retrieve the username field and password field from the database and match
the 2 usernames if they match i have another if statements that matches
$EncryptedPassword with the encrypted password saved in the MySQL database.
if they match then it retrieves other info about the user and displays it on the very same page (welcome.php)

This is the example i followed.
http://www.w3schools.com/PHP/php_forms.asp

All good and well i can log in using the username and password if correct if in-correct it directs you to a page telling you 'Incorrect username and password'

Ive never coded in php before so i dont even know if this is secure or not. (Anyway this is just for learning...)

Now lets say i want to update info of the currently logged in user, lets say the user's surname.
How do i re connect to the SQL server with the credentials sent by index.php cus the variables will be blank, is isnt there a global place where this can be stored in memory ? or should i store it in the URL
somehow ? Encrypted obviously ?
Post Reply