Jump to content


Photo

Using php include and if


  • Please log in to reply
11 replies to this topic

#1 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 07 March 2006 - 08:36 PM

I am making a 3 column website. The left/right column are static through-out the site and the center column is the main column that changes. To make it easier I created it in php and then use a php include for the center part. I would like to make a Accept and Do Not Accept link/button that they can select and it would then show the rest of the page. I know how to make it so I can do accept/not accept and then it would redirect it. However I would like it to instead just include the file if they click yes, but redirect if they say no.
<?

if ($YES) {

include("test.htm");

} else {

$URL="http://www.example.com";

header ("Location: $URL");

}
?> 
I believe something like that would work (probably some syntax wrong somewhere). Where do I put in the $YES choice, something like Accept and I Do Not Accept part.

Can someone help me out? Am I on the right track?
ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#2 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 07 March 2006 - 09:58 PM

Yes you are on the right track for processing the asccept/do not accept. Is it the actual accept/do not accept part that posts the to $yes that you want help with? if so are you wanting to do it as a form where you have submit buttons or use hyperlinks?

#3 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 08 March 2006 - 08:29 PM

Yes I am for some reason having a complete brain-ism and can't seem to figure out how to properly set it up. Hyperlinks or buttons would work, doesn't really matter to me. I could simply put Yes and No in a hyperlink and basic HTML, but not sure how to write it so it translate the click on the link as Yes or No. Maybe using a form to have a submit and no button or something.
ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#4 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 08 March 2006 - 09:55 PM

if you want to use a hyperlink you can use something like.

<a href='index.php?continue=1'>Yes</a><a href='index.php?continue=0'>No</a>
//then where you want to check what happened:
if (isset($_GET["continue"])
{
     if ($_GET["continue"] == 1) require_once("page.php");
     else //do something else
}


example of way to do it with a form

<form name='foo' method='post'>
       <select name='bar'>
              <option value='1'>Yes</option>
              <option value='0'>No</option>
       </select>
       <input type='submit'>
</form>


if (isset($_POST["submit"]))
{
      if ($_POST["bar"] == 1) require_once("page.php");
      else //do something else
}

hope that helps
call me a safe bet, i'm betting i'm not

#5 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 14 March 2006 - 04:39 PM

The syntax seems wrong but I should be able to correct it to get to work. If I do it that way though, will it work like a php include would? Or would it just redirect it to that page?

I am using an include because it pulls the main page and then places it basically within the cell it is supposed to go. If it just redirects then it would break out of the format for the website.
ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#6 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 15 March 2006 - 09:10 PM

I have tried to do this but still can't get it to work. I have tried both:

<?

<a href="index.php?continue=1">Yes</a><a href="index.php?continue=0">No</a>

if (isset($_GET["continue"])
{
     if ($_GET["continue"] == 1) require_once("page.php");

} else {

echo "test";

}

?>

and

<form name="foo" method="post">
       <select name="bar">
              <option value="1">Yes</option>
              <option value="0">No</option>
       </select>
       <input type="submit">
</form>

<?

if (isset($_POST["submit"]))
{
      if ($_POST["bar"] == 1) require_once("page.php");
      
} else {

echo "You have selected No";

}

?>

ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#7 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 15 March 2006 - 09:15 PM

What output are you getting? Are you getting errors? Have you included that page anywhere else in the script?
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#8 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 18 March 2006 - 06:15 PM

When I use this:
<form name="foo" method="post">
       <select name="bar">
              <option value="1">Yes</option>
              <option value="0">No</option>
       </select>
       <input type="submit">
</form>

<?

if (isset($_POST["submit"]))
{
      if ($_POST["bar"] == 1) require_once("page.php");
      
} else {

echo "You have selected No";

}

?>

I receive no errors. What do you mean have I included it anywhere else in the script? The only thing I have been using was what is above, that is it. Should it have been combined with something?

It loads page agree.php which has the code, there is drop down menu with yes and no. Selecting yes doesn't take it to page.php. It automatically echoes the selected No, when I only want it to do or say that action if they select no.

[a href=\"http://www.pcxdesigns.com/temp/agree.php\" target=\"_blank\"]Test Example[/a]
ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#9 theverychap

theverychap
  • Staff Alumni
  • Advanced Member
  • 78 posts
  • LocationDevon, England

Posted 18 March 2006 - 09:02 PM

<?php
// form is submitted
if ( isset($_POST['submit']) )
{
    $agree = $_POST['agree'];
    if ( $agree == 'yes' ) 
    {
        header('location: page.php');
    }
    else if ( $agree == 'no' )
    {
        echo 'you have selected no<br/>';
    }
    else
    {
        echo 'form not submitted yet<br/>';
        show_form();
    }
}


function show_form();
{
    ?>
    <form name="question" action="" method="post">
       <select name="agree">
              <option value="yes">Yes</option>
              <option value="no">No</option>
       </select>
       <input type="submit" name="submit" value="continue" />
    </form>
    <?php
}

?>


#10 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 18 March 2006 - 09:17 PM

Thank you for the help.

I receive the following error now:
Parse error: parse error, unexpected ';', expecting '{' in /home/protocul/public_html/temp/agree.php on line 25

Line 25:
function show_form();

If I removed the ; in line 25, I don't receive an error anymore but when I load the page it is a blank page.
ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#11 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 March 2006 - 10:19 PM

Anyone have any ideas on this? Thank you.
ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast

#12 Shadow Wolf

Shadow Wolf
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 30 March 2006 - 04:49 PM

I am probably not understanding it fully because I kept receiving a blank white screen. So I went back to the guides and tutorials and started from scratch again, creating my test file called form.php. I think I found out what happened. I was able to get it to work in my test. It would load the page, show the question and have a yes and no option. If I selected no it just said you have selected no. If I selected yes, it would then include xxx.htm file.

The problem was that it remembers what it did. So if I try to back to the form.php URL it would automatically include the xxx.htm file because that was what was chosen before. If I chose no then it would say I chose no. It isn't giving me the option each time I go to the form URL, it just remembers my last choice.

Example:
[a href=\"http://www.pcxdesigns.com/temp/form.php\" target=\"_blank\"]http://www.pcxdesigns.com/temp/form.php[/a]

<?php
// form is submitted
if ( isset($_POST['submit']) )
{
    $agree = $_POST['agree'];
    if ( $agree == 'yes' )
    {
include 'index2.htm';
    }
    else if ( $agree == 'no' )
    {
        echo 'you have selected no<br/>';
    }

}
?>

Thank you for your interest. Do you agree to blah blah blah test.

<form name="question" method="post">
<select name="agree">
    <option value="yes">Yes</option>
    <option value="no">No</option>
</select>
<input type="submit" name="submit" value="continue" />
</form>

ProtoCultureX
"Words can completely change their meaning depending on the mind of the listener. They can change into good ones or into bad. Some words cannot be conveyed. What one thinks is good will not always lead to good results."

--------------------------

"May you have the hindsight to know where you've been, the foresight to know where you're going, and the insight to know when you're going too far." - - - Irish Toast




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users