Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

html - How do I pass data between pages in PHP?

In a nutshell on "page1.php" I have a calculator that consists of an HTML form, and then the PHP code totals the input and displays the total price. Below the price, it also displays a link to "page2.php" which contains an HTML form where they can enter their contact information. Upon submitting the form the selections they made on "page1.php" in the pricing calculator as well as the contact info on "page2.php" are emailed to me, and they are redirected to the home page.

In the email that is submitted to me, I receive the contact info from "page2.php", but I don't receive anything from "page1.php", so the variables are not getting correctly passed. In addition to the PHP on each page, I am using hidden values in an HTML form on "page2.php" to echo the data that was entered in the HTML form on "page1.php". I know that one of my issues is that I have a couple of $_GET fields when my form is "post".

However when I change it so that everything is $_POST, the calculator no longer works. I tried to put this altogether with different snippets of code suggested by others. The form on "page1.php" has 13 fields, named "one" - "thirteen". $total display the values of 1-13.

<?php
  $submit = $_GET['submit'];
  if($submit == "true")
  {
    $total = ($_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four']  + 
    $_POST['five'] + $_POST['six'] + $_POST['seven'] + $_POST['eight']+ $_POST['nine'] + 
    $_POST['ten']+ $_POST['eleven'] + $_POST['twelve']+ $_POST['thirteen']); 
    echo  " Your Price is $ " .number_format ($total, 2, '.', ','). "<BR>";
    echo ('">Get Your Project Started</a>');
  }
?>

The second form uses hidden values to echo the info from page1.php, and has three more fields named "name", "email" and "details".

<?php
  $to = "jessica@designs.com";
  $message = "Pages:$_POST[one]
";
  $message .= "Pages:$_POST[two]
";
  $message .= "Pages:$_POST[three]
";
  $message .= "Ecommerce:$_POST[four]
";
  $message .= "No Ecommerce:$_POST[five]
";
  $message .= "CMS:$_POST[six]
";
  $message .= "No CMS:$_POST[seven]
";
  $message .= "Audio or Video:$_POST[eight]
";
  $message .= "Flash Intro:$_POST[nine]
";
  $message .= "Image Gallery:$_POST[ten]
";
  $message .= "Graphic Design or Logo:$_POST[eleven]
";
  $message .= "Copy:$_POST[twelve]
";
  $message .= "Images:$_POST[thirteen]
";
  $message .= "Price Total:$_POST[total]
";
  $message .= "Name:$_POST[name]
";
  $message .= "Email:$_POST[email]
";
  $message .= "
";
  $message .= "
";
  $message .= "Details:$_POST[details]
";
  mail($to, $subject, $message, $headers) ;
  }
?>

So what would be the correct PHP to put on "page1.php" and "page2.php"? Sorry the code is such a mess, if anyone could point me in the right direction, that would be great.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PHP is stateless unless you save things in session (which isn't secure right?)

You would need to make page2.php read all the values from page1.php and store them either in a server side state (session) or a client state (cookies or maybe hidden fields in the form)

If you want any of this secure or a secret, then you have to consider all of that as well. Nothing I explained is secret or secure in any way.

EDIT: here is an example of page1.php that sends the values of page1.php to page2.php as get parameters. You can do this with hidden fields, cookies or sessions as well.

What is important to remember is that page2.php is totally unaware of page1.php, and can't get to the values like you could it forms programming. Each page starts and ends it's life by the time you see a full web page, so you have to use some extra tricks to keep values. Those tricks are sessions, cookies, form posts, or gets.

<html>
<head>
<title>Page 1</title>
</head>
<body>
<?php
//set defaults assuming the worst
$total = 0;
$one =0;
$two=0;

//verify we have that value in $__POST
if (isset($_POST['submit']))
{
    $submit = $_POST['submit'];
    //If it is true, try some math
    if($submit == "sub-total")
        {
            if (isset($_POST['one']))
            {   
                $one = $_POST['one'];
                //Add checks to see if your values are numbers
                if ( ! is_numeric($one)) { $one = 0;}
            }

            if (isset($_POST['two']))
            {
                $two = $_POST['two'];
                if ( ! is_numeric($two)) { $two = 0;}
            }
            $total = $one + $two;
            echo " Your Price is $ " .number_format ($total, 2, '.', ','). "<BR>";
        }
    if($submit == "submit" )
    {
        //go to page two, with the total from page1.php passed as a $__GET value
        header("Location: page2.php?total=".$total);
    }
}
?>
    <form method="post" action="page1.php?submit=true">
        <input type="text" name="one" id="one" value="<?=$one?>"/>
        <input type="text" name="two" id="two"  value="<?=$two?>"/>
        <input type="submit" name="submit" value="sub-total" />
        <input type="submit" name="submit" value="submit" />
    </form>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...