user828353
user828353

Reputation: 33

Url from form using php

I am trying to create a link to include variables from a form. The script below works put only includes the first variable: http://example.com/abc.php?id=2

I want it to send: http://example.com/abc.php?id=2&name=zac

PHP code shown below:

$base = 'http://example.com/abc.php';
$id=$_GET['ID'];
$name=$_GET['Name'];

$data = array(
    'id' => $id,
    'name' => $name,
);

$url = $base . '?' . http_build_query($data);

header("Location: $url");
exit;

Upvotes: 0

Views: 1101

Answers (1)

Daniel G
Daniel G

Reputation: 549

You could make your life a bit easier by just sending the form via $_GET and it will redirect to the URL like you're wanting. Here's an example:

<form action="http://example.com/abc.php" method="GET">
  <input type="text" name="ID" />
  <input type="text" name="Name" />
</form>

This would send the user to: http://example.com/abc.php?ID=id_value&Name=name_value

Note: This will send all form variables with a value set.

Upvotes: 1

Related Questions