mehdi
mehdi

Reputation: 45

some simple question in php

i have some simple question. 1-how can i change php pages in a specify points? for example

if($flag)
//go to 1.php
else 
// go to 2.php

what i replace instead go to ?.php

2-

<form action="index.php" method="post">
<input type="button" name ="submit" value="comfirm">

if i click on button index.php is execute if there is any way in particular condition that i want index.php is called

for example we have two text fields and a button user must fill both of text and click on button until goes to next pages but if user fill one of text in must not go but in second way index.php must not called.

Upvotes: 0

Views: 113

Answers (5)

Tommy
Tommy

Reputation: 1267

if ($flag) {
    header("Location:1.php");
    exit(0);
}
else {
    header("Location:2.php");
    exit(0);
}

For the second question you can either use Javascript (not all users have javascript enabled, so it's not guaranteed to work), and check the value of the fields, or hava a serversite check and redirect the user with header, as above.

Upvotes: 3

jenkin90
jenkin90

Reputation: 357

I think this is the best way because you have not to fight with the possible lost of the $_POST[]

if($flag)
   require '1.php';
else 
   require '2.php';

Upvotes: 1

Mahesh
Mahesh

Reputation: 34625

For form validations, you need to use javascript.

 <head>
   <script type="text/javascript">

     function validateForm()
     {
         // Conditions to check whether the text box is empty or not.
     }

   </script>
 </head>

<form action="index.php" method="post">
<input type="button" name ="submit" value="comfirm" onsubmit = " return validateForm() ">

If validateForm() returns true, then only on button click it goes to index.php

Upvotes: 1

Naftali
Naftali

Reputation: 146302

Try this:

if($flag) {
  //go to 1.php
  header("Location: 1.php");
  exit(0);
}
else  {
  //go to 2.php
  header("Location: 2.php");
  exit(0);
}

And for the second part you might want to use some client side script to double check the inputs and a server side script to double double check them

Upvotes: 2

Jonathan
Jonathan

Reputation: 76

I like to use META refresh..

<meta http-equiv="refresh" content="3;url=somewhere.php">

3 is the number in seconds before it redirects the viewer

Upvotes: 1

Related Questions