Viewed   103 times

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:t$_POST[one]n";
  $message .= "Pages:t$_POST[two]n";
  $message .= "Pages:t$_POST[three]n";
  $message .= "Ecommerce:t$_POST[four]n";
  $message .= "No Ecommerce:t$_POST[five]n";
  $message .= "CMS:t$_POST[six]n";
  $message .= "No CMS:t$_POST[seven]n";
  $message .= "Audio or Video:t$_POST[eight]n";
  $message .= "Flash Intro:t$_POST[nine]n";
  $message .= "Image Gallery:t$_POST[ten]n";
  $message .= "Graphic Design or Logo:t$_POST[eleven]n";
  $message .= "Copy:t$_POST[twelve]n";
  $message .= "Images:t$_POST[thirteen]n";
  $message .= "Price Total:t$_POST[total]n";
  $message .= "Name:t$_POST[name]n";
  $message .= "Email:t$_POST[email]n";
  $message .= "n";
  $message .= "n";
  $message .= "Details:t$_POST[details]n";
  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.

 Answers

4

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>
Monday, August 8, 2022
1

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, August 20, 2022
2

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

The docs for Intents has more information (look at the section titled "Extras").

Friday, October 7, 2022
 
bjunix
 
3

The natural way to persist the data among different submits is by using the Datastore.
If, for exercise, you don't want to use the datastore, you are forced to pass the data to the view and read it back using some hidden fields between each web request.

Let's see an easy quick&dirty example: a web app that sums integers one by one.

application.py

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class Sum(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        template_values = {"total":0}
        self.response.out.write(template.render(path, template_values))

    def post(self):    
        value = self.request.get("value")
        total = self.request.get("total")
        total = int(total) + int(value)
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        template_values = {"value":value,
                           "total":total
                          }
        self.response.out.write(template.render(path, template_values))

application = webapp.WSGIApplication(
                                 [('/', Sum),
                                  ('/sum', Sum)],
                                 debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

To pass the data to the index.html view, the template.render function is used passing a dictionary variable that carries the result to the view:

template.render(path_to_the_view, template_values)

index.html

<html>
<body>
  <form action="/sum" method="post">
    <p>
    <label for="value">Value to sum:
    <input type="text" name="value"><br>
    <input type="submit" value="Submit">
    <input type="hidden" name="total" value="{{ total }}">
    </p>
  </form>
  Last value submitted: {{ value }}| Total:{{ total }}
</body>
</html>

The index.html view uses the dictionary keys value and total to show the result and to submit back the total to the controller using an hidden field.

And here is the result:

Saturday, December 24, 2022
 
2

I'm just going to go ahead and make this an answer.

000webhost most likely have a default 2M upload max.

You can override that using .htaccess and changing its value to

php_value memory_limit 30M
php_value post_max_size 100M
php_value upload_max_filesize 30M

for example and placed in the root of your server. Since you are unable to modify their php.ini file.

As per a few findings done on Google for you.

http://www.000webhost.com/forum/customer-assistance/202-file-upload-limit-2mb-set-php-ini.html

as it's stated We do not allow to upload big files (where file size is more than 5 MB) on server In a backend of my component (Pictures Gallery) there is a statement that Upload file limit is 2Mb - set by php.ini I use Joomla. Does anybody know how to increase it up to 5Mb? Is it on server or somewhere in my file system?

and

the limit on this hosting for uploading files is 2Mb, you can check in phpinfo().

Plus, just for argument's sake:

Form requires a valid enctype when uploading; you don't have one.

Change your form to this:

<form action="upload.php" method="post" enctype="multipart/form-data">
Saturday, August 27, 2022
 
fei_han
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :