Jump to content

Archived

This topic is now archived and is closed to further replies.

Shadow Wolf

Using php include and if

Recommended Posts

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.
[code]
<?

if ($YES) {

include("test.htm");

} else {

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

header ("Location: $URL");

}
?>
[/code]
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
if you want to use a hyperlink you can use something like.

[code]
<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
}

[/code]

example of way to do it with a form

[code]
<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
}
[/code]

hope that helps

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I have tried to do this but still can't get it to work. I have tried both:

[code]<?

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

}

?>[/code]

and

[code]<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";

}

?>[/code]

Share this post


Link to post
Share on other sites
What output are you getting? Are you getting errors? Have you included that page anywhere else in the script?

Share this post


Link to post
Share on other sites
When I use this:[code]<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";

}

?>[/code]

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]

Share this post


Link to post
Share on other sites
[code]<?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
}

?>[/code]

Share this post


Link to post
Share on other sites
Thank you for the help.

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

Line 25:[code]function show_form();[/code]

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.

Share this post


Link to post
Share on other sites
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]

[code]<?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>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.