I would like to save the text from a textbox. I want to get the value of idForo in the URL.
http://localhost/foro1/indice.php?idForo=1
I get the error
Notice: Undefined index: idForo in C:\xampp\htdocs\foro1\indice.php on line 44
The controller
if (isset($_GET['newAnswer'])){
$idForo=$_POST['idForo']; /*line 44*/
$newAnswer=$_POST['newAnswer'];
//Save new answer using PDO
header("Location:indice.php?idForo=$idForo");
}
The form
<form method='post' action='indice.php'>
<textarea rows='7' cols='60' name='newAnswer' required></textarea> <br>
<input type='submit' class='responder' value='Responder' name='comentario'>
</form>
I appreciate your help
To get variables from the query string, you need to use $_GET
. $_POST
represents data that is sent to the script via the HTTP POST method.
You're also translating (?) the post variable name for the textarea for some reason?
Try:
if (isset($_POST['nuevaRespuesta'], $_GET['idForo'])) {
$idForo = $_GET['idForo']; /*line 44*/
$newAnswer = $_POST['nuevaRespuesta'];
//Save new answer using PDO
header("Location: indice.php?idForo=$idForo");
}
To get the value of a variable, you need to use $_GET
in your example, you just need to do this:
$idForo = $_GET['idForo'];
this will create a new variable $idForo
from the variable in your URL. Hope this helps.
To get the variable into your form, you would need to change the first line to this:
<form method='post' action='indice.php?idForo=<?php $idForo ?>'>
idForo might not be in the $POST array, so use the following to allow for that scenario:
if(isset($_GET['idForo'])) {
$idForo = $_GET['idForo'];
} else {
$idForo = null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.