How to set and get values from an url - php

I would like to set values on an url like this:
<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note'&user='$username'>
Then be able to grab then from the recieving page.
Right now all im getting is this when clicked:
http://localhost/readnotes/viewyournote.php?chapter=

I don't how you embed your link in your code, but if it is outside of <?php ?> tags, then you have to do:
<a href="http://<?php echo $data_url ?>/viewyournote.php?chapter=<?php echo $name_of_chapter ?>&note=<?php echo $id_note ?>&user=<?php echo $username?>" >
if it is inside these tags, you can also do:
echo "<a href='http://$data_url/viewyournote.php?chapter=$name_of_chapter&note=$id_note&user=$username?' >";
You can get these values on the recieveing page with $_GET['variable_name_here'], e.g. $_GET['chapter'].

Use Query string
$val = "yourvalue" ;
$url "http://localhost/readnotes/viewyournote.php?chapter=$val";
Now $val is passed to specified url .
There you could get it by using $_GET['chapter'] , It will give you "yourvalue"

<a href='http://$data_url/viewyournote.php?chapter=<?php echo $name_of_chapter; ?>&note=<?php echo $id_note; ?>&user=<?php echo $username; ?>>

Replace your line with
<?php
echo "<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note' user='$username'>";
?>
On the receiving end use
<?php
$name_of_chapter = $_GET['chapter'];
...
?>

Related

how to access value which passed through url?

I have trying to access value of variable from one page to another by using following code:-
page1.php
<td>
echo $var12=$row['p_id'];
echo 'Received';
</td>
receive_branch_confirmation.php
<?php echo $_GET['prop_id']; ?>
My previous error solved but now it is just print ",$var12,"
please tell me where is my mistake
As you are using echo for displaying the a tag you no need to use echo or close and open php tags.
It is enough if you concatenate the variable that you are going to send.
$var12 = '2';
echo 'Received';
Then your URL will look like as follows
http://domain.com/receive_branch_confirmation.php?prop_id=2
And in the receive_branch_confirmation.php you can access the variable passed with the help of $_GET or $_REQUEST
<?php
echo 'Request Value: '.$prop_id = $_REQUEST['prop_id']; // this will result the output as 2
echo '<br>';
echo 'Get Value'.$prop_id = $_GET['prop_id']; // this will result the output as 2
?>
Output:
Request Value: 2
Get Value: 2
Another Example:
If the URL is http://domain.com/page.php?var1=apple
then if you need to access the variable as follows in page.php.
<?php
$u1 = $_GET['var1'];
$u2 = $_REQUEST['var1'];
echo $u1; //would output "apple"
echo $u2; //would output "apple"
?>
page1.php
<td>
<?php echo '<a href=receive_branch_confirmation.php?prop_id='.$var12.'>Received</a>'; ?>
</td>
receive_branch_confirmation.php
<?php
echo $_GET['prop_id'];
?>
It should be
Received
in html page and
<?php
echo $_GET['prop_id'];
?>
in php page
and always try to separate html from PHP
You need to update your page1.php as below
echo 'Received';
and get it like this
<?php
echo $_GET['prop_id'];
?>
Try this in html
<td>
<a href="receive_branch_confirmation.php?prop_id="<?php echo $var12;?>>Received</a>
</td>
in php side
<?php
echo $_GET['prop_id'];
?>
You need to change the $_GET value.
<?php
echo $_GET['prop_id'];
?>
Now it's looking for the prop_id instead of the value

Passing variable to next page through url

Here I am counting the number of persons a profile owner is following, and the output number is link to open a new page were all persons being followed will be displayed, the count works well and gives the number of persons being followed but now to complete my next page say following.php where all persons being followed will be displayed I need to pass variable $id to that page for which I want to use url method, please help me on this.
<?php
$checkfollowing =$db->prepare("SELECT * FROM follow_user WHERE uid_fk=:id");
$checkfollowing->execute(array(':id'=>$id));
$count = $checkfollowing->rowCount(); ?>
<a href='following.php' style='text-decoration:none;margin-left:40px;'><?php print $count; ?></a>
you can pass value by url and get the value on other page by using $_GET method.
check the below code.
<a href='following.php?id=<?php echo $id ; ?>' style='text-decoration:none;margin-left:40px;'><?php print $count; ?></a>
<a href='following.php?id=<?php echo $id; ?>' style='text-decoration:none;margin-left:40px;'><?php print $count; ?></a>
you can pass a variable through url by specifieng your variable at end of your url. like this
<a href='following.php?count=<?php echo $count; ?>' style='text-decoration:none;margin-left:40px;'><?php print $count; ?></a>

How to add PHP variables to a href url

Im trying to add a PHP variable to a href url
This is the code:
PHP
$uid= $_SESSION['userid'];
HTML
<a href=http://example.com/uid= <?php echo ".$uid."?> /a>
How do you add, when I do it, this is what it redirect too: http://example.com/uid=.
Try this,
<?php
#session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a>
echo "link description";
Perhaps something like this:
print '<a href=http://example.com/?uid=' . $uid . '>Link</a>';
try this
<?php
$url = 'https://www.w3schools.com/php/';
?>
PHP 5 Tutorial
Or for PHP 5.4+ (<?= is the PHP short echo tag):
PHP 5 Tutorial
or
echo 'PHP 5 Tutorial';
This is one of the most helpful
echo "<td>".($tripId != null ? "<a target=\"_blank\"href=\"http://www.rooms.com/r/trip/".$tripId."\">".$tripId."</a>" : "-")."</td>";
It will work!
This will work
<a href=http://example.com/?uid= <?php echo $uid ?> Myurl</a>
Try this:
<a href=http://example.com/uid= <?=$uid?> /a>
You're doing a couple things wrong.
In HTML you should quote attribute values such as href
In PHP you're not concatenating anything, just echoing
You forgot the link text
For readability, you can (probably) use the <?= shorthand instead of <?php echo
<a /a> is broken tag syntax. Should be <a>...</a>
End result is this:
Link Text

Check for/insert http in PHP/MySQL echo URL variable

I currently have a dynamic PHP page that displays values from a database to display a URL like this:
<a target="_blank" href="<?php echo($row['Virtual_Tour']); ?>"><?php echo($row['Virtual_Tour']); ?></a>
This works great EXCEPT when the data does not include a http://. I have not control over the data. How do I test to see if the echo starts with http:// and if not inserts that as well to guarantee the URL is properly formatted?
if(substr($row['Virtual_Tour'],0,7)=="http://")
{
//starts with http://, no formatting needed
}
else
{
$row['Virtual_Tour']="http://".$row['Virtual_Tour'];
}
You can just remove the exist http://, if it exists, and put a newone like below code
<a target="_blank" href="<?php echo "http://".str_replace("http://","",$row['Virtual_Tour']); ?>"><?php echo "http://".str_replace("http://","",( $row['Virtual_Tour']); ?></a>
you can do this also with https:// like
<a target="_blank" href="<?php echo "http://".str_replace("https://","", str_replace("http://","",$row['Virtual_Tour'])); ?>"><?php echo "http://". str_replace("https://","",str_replace("http://","",( $row['Virtual_Tour'])); ?></a>
If I get it correctly, if your url does not contain http:// you want to add it, since some answers provide solution to show it only like this:
http://something.com.
If I am right, you could use this:
$string = "something.com";
$string = substr($string,0,6) != 'http://' ? "http://".$string : $string;
echo $string;
working example

How to pass a query string value to a page loaded via AJAX?

I am trying to put a variable into an AJAX-loaded page link I can call it on the content it loads?
I have tried:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav={$array['username']}', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»{$array['username']}</i>\n";
echo "</div>\n";
echo "</a><br />\n";
I tried putting the link inside and outside the php code but neither of them worked, I also tried a few differents ways of calling it including $array['username'] All I need is a way I can define the variable "nav" as username (which is set in a database) so I can use it on that content that is called by the link.
Is it even possible?
I am not sure I completely understood your question.
But perhaps you want to try this out:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav=<?php echo $array['username'];?>', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»", $array['username'], "</i>\n";
echo "</div>\n";
echo "</a><br />\n";

Categories