I'm having an issue that's been driving me crazy for a while now because I just can't figure it out.
I want to echo something like "succeed" if the user creates a post from a form.
This is what i got so far in my db-querys file:
if ($_REQUEST) {
$name = $_REQUEST['name'];
$price = $_REQUEST['price'];
$dateofevent = $_REQUEST['dateofevent'];
$time = $_REQUEST['time'];
$textinfo = $_REQUEST['textinfo'];
$leg = $_REQUEST['leg'];
$sql= $dbh->prepare("INSERT INTO events(name, dateofevent, time, price, leg, textinfo)
VALUE (:name, :dateofevent, :time, :price, :leg, :textinfo)");
$sql->bindParam(':name', $name);
$sql->bindParam(':dateofevent', $dateofevent);
$sql->bindParam(':time', $time);
$sql->bindParam(':price', $price);
$sql->bindParam(':leg', $leg);
$sql->bindParam(':textinfo', $textinfo);
$sql->execute();
$url = $_POST['name'];
header('Location: events.php?'.$url);
}
Then I've tried using $_GET
to echo out something if the url is correct.
This is the code in my view-file so far:
if(isset($_GET[$url])) {
echo "success";
}
Here I get undefined variable $url and the echo doesn't work, nothing shows.
I've also tried something like:
$url = 'path/to/event.php';
if (!$_SERVER['REQUEST_URI'] == $_SERVER[$url]) {
echo "success";
}
And here I get undefined index path/to/event.php and the echo doesn't work aswell, nothing shows.
Can anyone please help me with my problem? I'm new in php so can't get any further with this, I'm stuck.
$array = array('foo' => 'bar');
$key = 'foo';
echo $array[$key]; // outputs 'bar'
That's what you're trying to do there, which obviously doesn't work because $url
is not a defined variable. In fact, since the name value doesn't have a key in the URL it's associated with, you can't get it that way from $_GET
at all.
The simplest solution for you is to add a key in the URL:
header('Location: events.php?name=' . urlencode($_POST['name']));
^^^^^
and get the value by key:
$name = $_GET['name'];
It's odd though to use a user-entered name as is for a primary id to something in the database. You should rather use the autoincrementing numeric id of the record in the database.