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
PHP Warning: PHP Startup: Unable to Load Dynamic Library 'Pdo_Mysql.So'
PHP Float Calculation 2 Decimal Point
Do I Need to Escape My Variables If I Use MySQLi Prepared Statements
PHP Array, Are Array Indexes Case Sensitive
Setting a Cookie in an Ajax Request
Saving File Using Curl and PHP
Differencebetween $_Files["File"]["Type"] and End(Explode(".", $_Files["File"]["Name"]))
Php: Populating an Array with the Names of the Next 12 Months
Laravel 5.3 - How to Add Sessions to 'Api' Without Csrf
PHP "Header (Location)" Inside Iframe, to Load in _Top Location
How to Use a PHP Includes Across Multiple Directories/Sub Directories with Relative Paths
JavaScript Cookies VS PHP Cookies
Calling PHP Function from Jquery
Pdoexception' with Message 'Sqlstate[22001]: String Data, Right Truncated: 0
Mysqli Commands Out of Sync; You Can't Run This Command Now
Echo PHP Variable from JavaScript
Warning: MySQL_Num_Rows() Expects Parameter 1 to Be Resource, Boolean Given