PHP Passing Parameters via Url

PHP passing parameters via URL

You should use .htaccess

Here's an example:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=$1&var2=$2 [NC,L]

This basically means that if the URL is formatted according to the regular expressions above (number - slash - alphanumeric,dashes or underscores) than the content should be displayed from index.php while passing the file two parameters called var1 and var2, var1 having the value of the number and the second having the value of what's after the first slash.

Example:

mysite.com/20/this_is_a_new_article/

Would actually mean

mysite.com?var1=20&var2=this_is_a_new_article

Of course, in your index.php file you can simply take the values using

$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

Cheers!

How to pass a PHP variable using the URL

In your link.php your echo statement must be like this:

echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';

Then in your pass.php you cannot use $a because it was not initialized with your intended string value.

You can directly compare it to a string like this:

if($_GET['link'] == 'Link1')

Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.

Pass variables in url using POST

You need to use the GET method if you want to pass parameters in the URL. POST is a different method and it's not possible to represent or reference POST data in the URL.

Some reading on the definition and difference between post and get: http://www.w3schools.com/tags/ref_httpmethods.asp

Pass parameter from URL to a php function in an api php

You have an error in the code. You are executing a "prepared statement" but with the wrong statements. Try it like this:

function getOrders($last_oid)
{
try {
$orders = array();
$stmt = $this->con->prepare("SELECT oid, uid, order_num, create_date, status_date FROM orders WHERE oid > ?");
// I assume $ last_oid is an integer.
$stmt->bind_param("i", $last_oid);
if ($stmt->execute()) {
$result = $stmt->get_result();

// use the while loop to load the result set
while ($row = $result->fetch_assoc()) {
array_push($orders, array(
'oid' => $row['oid'],
'uid' => $row['uid'],
'order_num' => $row['order_num'],
'create_date' => $row['create_date'],
'status_date' => $row['status_date']
));
}
return $orders;
}

} catch (Exception $ex) {
print $ex->getMessage();
}
}

Passing variables to another page with url - PHP

You set $_SESSION['status'] to 0 and then set it to 1, so it will always be 1. Additionally, the link you click has nothing to do with the session vars.

Page 1:

Normally to get just one variable from one page to another via hyperlink you would add it to the URL as a query parameter and access it using $_GET:

<?php
echo '<a href="page2.php?status=0">No</a>';

echo '<a href="page2.php?status=1">Yes</a>';
?>

Page 2:

You also need to check if $_GET['status'] is set:

<?php
if (isset($_GET['status']) && $_GET['status'] == 0) {
echo "No !";
}
elseif (isset($_GET['status']) && $_GET['status'] == 1) {
echo "Yes !";
}
else {
echo "NOT set !";
}
?>

Now, if you want it to be accessible on other pages without passing in the URL, then set it as a session var:

    session_start();
$_SESSION['status'] = $_GET['status'];


Related Topics



Leave a reply



Submit