Add Two Numbers

PHP and HTML

PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it’s important you learn how to retrieve variables from external sources. The manual page on this topic includes many examples as well.

  • HTML interpretation. In order to specify a random string, you must include it in double quotes, and htmlspecialchars() the whole value.
  • URL: A URL consists of several parts. If you want your data to be interpreted as one item, you must encode it with urlencode() .

Example #1 A hidden HTML form element

Note: It is wrong to urlencode() $data , because it’s the browsers responsibility to urlencode() the data. All popular browsers do that correctly. Note that this will happen regardless of the method (i.e., GET or POST). You’ll only notice this in case of GET request though, because POST requests are usually hidden.

Example #2 Data to be edited by the user

Note: The data is shown in the browser as intended, because the browser will interpret the HTML escaped symbols. Upon submitting, either via GET or POST, the data will be urlencoded by the browser for transferring, and directly urldecoded by PHP. So in the end, you don’t need to do any urlencoding/urldecoding yourself, everything is handled automagically.

Example #3 In a URL

Note: In fact you are faking a HTML GET request, therefore it’s necessary to manually urlencode() the data.

Note: You need to htmlspecialchars() the whole URL, because the URL occurs as value of an HTML-attribute. In this case, the browser will first un- htmlspecialchars() the value, and then pass the URL on. PHP will understand the URL correctly, because you urlencode() d the data. You’ll notice that the & in the URL is replaced by & . Although most browsers will recover if you forget this, this isn’t always possible. So even if your URL is not dynamic, you need to htmlspecialchars() the URL.

I’m trying to use an tag, but the $foo.x and $foo.y variables aren’t available. $_GET[‘foo.x’] isn’t existing either. Where are they?

When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:

Читайте также:  Php curl authorization oauth

When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables: foo.x and foo.y .

Because foo.x and foo.y would make invalid variable names in PHP, they are automagically converted to foo_x and foo_y . That is, the periods are replaced with underscores. So, you’d access these variables like any other described within the section on retrieving variables from external sources. For example, $_GET[‘foo_x’] .

Note:

Spaces in request variable names are converted to underscores.

How do I create arrays in a HTML ?

To get your result sent as an array to your PHP script you name the , or elements like this:

Notice the square brackets after the variable name, that’s what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It’s also possible to assign specific keys to your arrays:

Note:

Specifying array keys is optional in HTML. If you do not specify the keys, the array gets filled in the order the elements appear in the form. Our first example will contain keys 0, 1, 2 and 3.

How do I get all the results from a select multiple HTML tag?

The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. I.e.

var=option1 var=option2 var=option3

Each option will overwrite the contents of the previous $var variable. The solution is to use PHP’s «array from form element» feature. The following should be used:

This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0] , the next $var[1] , etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.

Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it’s numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example:

variable = document.forms[0].elements['var[]'];

How can I pass a variable from Javascript to PHP?

Читайте также:  PHP Basics

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a «stateless» protocol, the two languages cannot directly share variables.

It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this — it allows PHP code to capture screen height and width, something that is normally only possible on the client side.

Example #4 Generating Javascript with PHP

if (isset( $_GET [ ‘width’ ]) AND isset( $_GET [ ‘height’ ])) // output the geometry variables
echo «Screen width is: » . $_GET [ ‘width’ ] . «
\n» ;
echo «Screen height is: » . $_GET [ ‘height’ ] . «
\n» ;
> else // pass the geometry variables
// (preserve the original query string
// — post variables will need to handled differently)

User Contributed Notes

  • FAQ
    • General Information
    • Mailing lists
    • Obtaining PHP
    • Database issues
    • Installation
    • Build Problems
    • Using PHP
    • Password Hashing
    • PHP and HTML
    • PHP and COM
    • Miscellaneous Questions

    Источник

    How to use $_GET and $_POST arrays in PHP

    GET and POST are two essential methods when a web communicates with the server. These methods determine whether the web is posting data to or getting data from the server.

    PHP which is a language that was created for making the web, give us a useful way to handle the data in process of Post or Get method. In this post, we’re getting to know about $_GET and $_POST array in PHP.

    There’s one thing you need to know that in a .php file, we can write HTML and PHP together, the PHP code must be written in between the tag ;

    How to pass variables in URL

    First of all, we need to know how to pass some variables or parameters in the URL.

    http://localhost/task1.php/?firstName=Sam&lastName=Nguyen
    1. To declare the variable, we use “?” mark
    2. then the variable name
    3. “=” sign to assign a value
    4. the last thing is the value itself
    5. If you have multiple variables, separating them by “&” mark.

    Using $_GET array in PHP

    Now that we know how to pass the variables in the URL, we’re going to get it in PHP using $_GET. $_GET is a built-in variable of PHP which is an array that holds the variable that we get from the URL.

    Since we passed some variable in the URL, we should know what is the name of variables to get it using the syntax $_GET[variable_name] ;

    echo $_GET['firstName'] //Sam echo $_GET['firstName'] //Nguyen use "echo" to print the value to web page.

    Validate variable in URL

    There are two things we will consider here: first if the variable is declared, second if the variable is blank.

    Using isset() to check if the variable is declared in URL, this is a function that returns boolean true if the parameter is detected in URL false if not. As long as the variable name is in the URL it will return true.

    http://localhost/task1.php/?firstName=Sam&lastName= isset($_GET['firstName']) // true isset($_GET['middleName']) // false isset($_GET['lastName']) // true

    As you can see in the code above, the lastName variable hasn’t been provided a valued but it still returns return true since isset() function detect the variable name in URL.

    Now we need to check if the variable is empty/blank or not. This is as simple as any other language by comparing the variable to an empty string

    http://localhost/task1.php/?firstName=Sam&lastName= $_GET['firstName'] == "" // false $_GET['lastName'] == "" // true

    Using $_POST array in PHP

    To use the Post method, we definitely need a form in our HTML file to handle data we want to post. The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.

    Consider the simple application below, we will demand the user to put in 2 number to add them together.

    The form in the code above has 2 data that we want to process that are stored in 2 input fields. Notice that these input fields have different name value. And the value of the name attribute is what we use to access the value of the input fields itself in PHP.

    Whenever the post method is triggered by clicking the submit button, PHP will get the value of the variable that we desire to catch.

    $_POST["num1"] // this will hold the value of input field with name="num1" $_POST['num2'] // this will hold the value of input field with name="num2"

    And then, we will echo/ print out the sum onto the web page

    echo $_POST["num1"] + $_POST['num2']

    Source code for $_POST:

    ADD 2 NUMBER

    Источник

Оцените статью