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

Asked

Viewed 16,275 times

4

I know there are GET and POST methods but I don’t know how to apply them.

I have a page called index.php and I have several Ivs that are generated in a while. When I click on a div I am redirected to a test.php page. how do I apply the data I picked up directly on this link and catch them later inside the page test.php?

the way I imagine it would be like test.php?tmpString='Teste' And to get the code later I have no idea :/

3 answers

4


Practical example by the GET method

This link will send the parameter by the GET method

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

To rescue 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 need to use the tag <form>

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

To rescue 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">

3

Sending by URL:

Teste.php? tmpString=teste

Taking data from the URL:

$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: is used to pick up data sent by URL, which was your problem.

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

  • While the post data is not visible in the URL, it is clearly visible in the request body HTTP, normally in format application/x-www-form-urlencoded (key value pairs, such as the query string) and therefore are no longer secure. Unless used HTTPS

  • parallel connection to the POST?

3

I know there are GET and POST methods but I don’t know how to apply them.

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

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

Secure - An order is considered secure if it does not change status on the Server

The GET method should be Indempotent and secure, meaning whenever you can get a resource without changing status, the GET method is a good candidate.

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

The DELETE method is Indempotent but it is not safe, you can make a request to delete a resource as many times as you want that the system should only delete it 1 time, but changes the state.

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

I don’t know PHP but I think user Santana gave an example of how to get data from query string.

  • +1 because you said something that most ignore and do not understand that this "is the web" (at least in the matter of websites): Understanding the semantics of HTTP methods is essential for programming web applications. Actually the minimum is to understand what is HTTP, request and response. This is something that is the basic, but totally necessary to know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.