How to send a data to another PHP page by URL?

4

I know there are GET and POST methods but I do not know how to apply them.

I have a page called index.php and I have several divs that are generated in a while. When I click on a div I'm redirected to a test.php page .. how do I apply the data I got directly to that link and then get it from inside the page test.php?

The form I imagine would be type test.php?tmpString='Teste' and to get the code later I have no idea: /

    
asked by anonymous 03.01.2016 / 10:59

3 answers

4

Practical example by GET method

This link will send the parameter by the GET method

<a href="test.php?tmpString=Teste">link teste</a>

To redeem the "tmpString" parameter on the "test.php" page:

<?php
if (isset($_GET['tmpString']))
    $tmpString = $_GET['tmpString'];
else
    $tmpString = null;

echo 'o valor de tmpString é: '.$tmpString;

Practical example by the POST method

To send by POST method through HTML, you must use the <form>

<form action="test.php" method="POST">
<input type="text" name="tmpString" value="Teste" />
<input type="submit" value="enviar" />
</form>

To redeem the "tmpString" parameter on the "test.php" page:

<?php
if (isset($_POST['tmpString']))
    $tmpString = $_POST['tmpString'];
else
    $tmpString = null;

echo 'o valor de tmpString é: '.$tmpString;

Note : A form <form> , can also send by GET method: <form action="test.php" method="GET">

    
03.01.2016 / 17:53
4
  

I know there are GET and POST methods but I do not know how to apply them.

Understanding the semantics of HTTP methods is essential for programming Web applications. I will try to explain the differences between GET and POST, but first you have to understand the following concepts:

Idempotente - Something that can be applied several times, always generating the same result. A simple example is the multiplication by 1. You can multiply any number by 1 as many times as you want it will always get the original number.

Secure - A request is considered secure if it does not change state on the Server

The GET method must be Indempotent and secure, ie whenever you can get a feature without changing state, the GET method is a good candidate.

The POST method is neither Indempotent nor secure, and can be used for example to change state in an object.

The DELETE method is Indempotent but is not secure, you can make a request to delete a resource as many times as you want the system to delete it only once, but change the state.

So if the request you are making to your test page is indempotent, secure, and you can use the query string to pass the data, you can use the GET method to do this.

I do not know PHP but I think Santana gave an example of how to get data from the query string.

    
03.01.2016 / 14:09
4

Sending by URL:

Test.php? tmpString = test

Getting URL data:

$tmpString = $_GET['tmpString'];

You can still use a page access if.

if($tmpString == teste {
    //  Código
} else {
    echo 'Error';
}

About GET and POST methods

GET: used to get data sent by the URL, which was your problem.

POST: Using it creates a parallel connection to send the data.

    
03.01.2016 / 13:12