We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,337 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Change script by using dropdown

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 .

4
Contributors
32
Replies
6 Days
Discussion Span
8 Months Ago
Last Updated
34
Views
Question
Answered
TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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).

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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');
  }
?>
TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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!">
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Is this solved now?

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

not yet

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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.

Javvy
Junior Poster
150 posts since Feb 2011
Reputation Points: 10
Solved Threads: 31
Skill Endorsements: 2

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

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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;
    }


?>
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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.

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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.

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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!

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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

TKO
Junior Poster in Training
70 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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!

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page rendered in 0.1201 seconds using 2.77MB