hello,
im tyring to change script from another page. i would like to change (a > true) to false and back if need be.
As a dropdown or radio selection, maybe using variables but everything i have tried i have no luck.

<?php
if (a > true ) print "a is true";
else include("test.php");

?>

thank you .

Recommended Answers

All 32 Replies

Hey, I think I get what you mean:

Let's say your radio button had the name of "option" and a value of either true/false

<?php

  $a = $_POST['option']; // this will either store true/false
  if($a)
  {
     echo 'a is true';
     //print ("A is true");
  }else{
    include ('test.php');
  }
?>

Hopefully this makes sense :)

Also, a note to when using the operator ">" this implies you mean more-than, but, boolean cannot be more than true.. It can simply be true or false (1 or 0).

Thanks for the reply,
so i tried this with no luck

page1.php

<FORM name ="form1" method ="post" action ="test2.php">

<Input type = 'Radio' Name ='option' value= 'true'
>ON
<Input type = 'Radio' Name ='option' value= 'false'
>OFF

<P>
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>

test2.php

<?php

  $a = $_POST['option']; // this will either store true/false
  if($a)
  {
     echo 'a is true';
     //print ("A is true");
  }else{
    include ('test.php');
  }
?>

try:

  <form method="post" action="test2.php">
    <input type="radio" name="option" value="TRUE">True</option>
    <input tpe="radio" name="option" value="FALSE">False</option>
    <input type="submit" name="submit" value="Go!">

if i pick either true or false it show (a is true), then on refresh it includes test.php...
thanks

Is this solved now?

not yet

Ok - Can you tell us what else needs to be done? I'm confused!

probably the $_POST['option'] comes back as a string.
change line 4 to

if (strcasecmp($a, "true")==0)

or simply echo $a and see what's the value and recreate the if-else statement.

ya sry
so this works untill i refresh the page, once refresh it like resets back to include ('test.php')

page1.php

<form method="post" action="page2.php">
<input type="radio" name="option" value="true">true</option>
<input type="radio" name="option" value="false">false</option>
<input type="submit" name="submit" value="Go!">

page2.php

<?php

  $a =  $_POST['option']; // this will either store true/false
  if($a)
  {
     echo 'a is true';
     //print ("A is true");
  }else{
    include ('test.php');
  }
?>

so what i am tring to get is if option true is selected to only show A is true.
but when false is selected include ('test.php').

hope this makes sense :)

thanks

on this same script can one change the variable to = content of a differant page ?
so i would write true or false to index.php
$a = $_POST['option']
with
$a = index.php

<?php

  $a =  $_POST['option']; // this will either store true/false
  if($a)
  {
     echo 'a is true';
     //print ("A is true");
  }else{
    include ('test.php');
  }
?>

thanks so much guys for your help.

I don't get what you mean..

You can't set $a = index.php and expect true or false, only if it's a weird function and it's returning either true/false.. So I think you mean:

<?php

  $a = (bool) $_POST['option']; // this will either store true/false
  if($a)
  {
     $a = "index.php";
     // redirect OR include $a
  }else{
    include ('test.php');
  }
?>

phorce thanks for that,
still once i refresh the script resets its self.
maybe i need to store/save $_POST['option'] so it will not go back.
maybe in session or something.. im not really sure
thanks

I don't get what you mean by 'resets' itself. Do you want it to remember what the option was so when the user re-visits the page, it will remember what the option was? If so, do this:

<?php
    ob_start();
    session_start();

    if(!isset($_SESSION['option']))
    {
        $a = (bool) $_POST['option'];
    }else{
        $a = $_SESSION['option'];
    }

    if($a)
    {
        $a = "index.php";
        // redirect OR include $a
        $_SESSION['option'] = true;
    }else{
      include ('test.php');
      $_SESSION['option'] = false;
    }


?>

thanks phorce
almost now with that script, the session seems to stay and will not include test.php at all.

all i am tryin to do is have one page have an on / off, button/radio/dropdown
then second page show test.php when its on and nothing when its off.

I don't get what you mean. Ok, so, the form SHOULD display everytime the user visits this webpage? If that's the case, I don't know exactly why we're storing the value as a session. Could you give me an example of how you want the form, maybe I could create it for you? Just a suggestion. Write our your specifications etc..

cool thanks
so basically im tring
to have 2 files.
page1.php should have my form
page2.php should show test.php unless page1.php is selected to off or false.

thanks so much for your help.

Sorry for late reply.

This is working perfectly fine for me:

<?php

    $option = (bool) $_POST['option'];

    if(!isset($option))
    {
        echo 'You have not submitted an option';
    }

    switch($option)
    {
        case 1:
        // re-direct to link 1;
            $new_link = "index.php";

        break;

        case 2: 
        // re-direct to link 2:
            $new_link = "test.php";
            include($new_link);
        break;

        default:
            echo 'Invalid option';
        break;
    }


?>

Or alternatively, try this (all one file):

<?php

    if(!isset($_POST['submitted']))
    {
        // form hasn't been submitted
        echo '
        <form method="post">
        <input type="radio" name="option" value="TRUE">True</option>
        <input tpe="radio" name="option" value="FALSE">False</option>
        <input type="submit" name="submit" value="Go!">
        <input type="hidden" name="submitted" value="TRUE">
        ';

    }else{

      switch($option)
      {
            case 1:
                // re-direct to link 1;
                $new_link = "index.php";

            break;

            case 2: 
            // re-direct to link 2:
                $new_link = "test.php";
                include($new_link);
            break;

            default:
                echo 'Invalid option';
            break;
        }
    }


?>

I realised SESSIONS were a bad idea since we were recoding the data, so, it would always remember it!

hmm weird man all i get is Invalid option on both true and false and test.php never shows up.

Sign :(!

Try this, change your form values so it has 0 or 1. 0 = false, 1 = true

<?php


    $option = $_POST['option'];

    if(!isset($option))
    {
        echo 'You have not submitted an option';
    }

    switch($option)
    {
        case 0:
            // FALSE
            echo 'This is false';

        break;

        case 1:
             // TRUE
             // re-direct to link 1;
            $new_link = "index.php";
            echo 'This is true';
        break;

        default:
            echo 'Invalid option';
        break;
    }


?>

E.g.

<form method="post">
        <input type="radio" name="option" value="1">True</option>
        <input tpe="radio" name="option" value="0">False</option>
        <input type="submit" name="submit" value="Go!">

Hopefully this will work, let me know!

Member Avatar for diafol

Sorry to butt in here, but wouldn't a checkbox be more appropriate for a true/false? Perhaps not, depending on the use case. If you cast the post data as boolean, you get the true/false:

<?php
if($_POST){
    $option = (bool) $_POST['option'];  

    if($option){
        header("Location: index.php");
    }

}?>
<form method="post">
    <input type="radio" name="option" value="1" checked="checked">True</option>
    <input type="radio" name="option" value="0">False</option>
    <input type="submit" name="submit" value="Go!">
</form>
commented: Thanks :)! You're right actually aha! +6

thanks
still on refresh returns to false..
maybe us session then make false Destroy the session ? just a thought...

<?php
$option = $_POST['option'];
if(!isset($option))
{
echo 'You have not submitted an option';
}
switch($option)
{
case 0:
// FALSE
echo 'This is false';
break;
case 1:
// TRUE
// re-direct to link 1;
$new_link = "test.php";
echo include 'test.php';
break;
default:
echo 'Invalid option';
break;
}
?>

    <form method="post">
    <input type="radio" name="option" value="1">True</option>
    <input type="radio" name="option" value="0">False</option>
    <input type="submit" name="submit" value="Go!">

Do you have an online example of what is happening when you re-fresh? A url or something?

pm sent

Member Avatar for diafol

By the way, the html I copied was wrong:

<form method="post">
    <input type="radio" name="option" value="1" checked="checked">True</option>
    <input type="radio" name="option" value="0">False</option>
    <input type="submit" name="submit" value="Go!">
</form>

Should be:

<form method="post">
    <input type="radio" id="optrue" name="option" value="1" checked="checked" /><label for="optrue">True</label>
    <input type="radio" id="opfalse" name="option" value="0" /><label for="opfalse">False</label>
    <input type="submit" name="submit" value="Go!">
</form>

@TKO

With regard to PMs, unless it's a personal one, please post all relevant info to the forum as this will help other users with a similar problem.

@diafol The user requires something different to what they are posting. He wants the script to remember the option that they selected, without using Database. Any suggestions other than cookies? (Obviously they would expire after a set amount of time)! I can't think of any other way.

Member Avatar for diafol

This is the reason why questions of this nature should be put to the forum as opposed to PM. This is related to the problem. WHen he mentioned 'refresh' I assumed he meant 'why isn't the form storing my data'.

The best way to my mind would be a session variable. You could use cookies of course. I find cookies a little fiddly and I tend not to use them very much anymore - especially following the ridiculous cookie legislation - not that that is relevant here.

Anyhow, seeing as TKO sent the PM to you, you should answer him. If he decides to post to the forum, it's open season. :)

the only reason pm was sent was for link to file.
no harm intended

@TKO, no harm made..

Are you clear what you want the script to do yet ha?

Member Avatar for diafol

@TKO - no prob.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.