This question already has answers here:
What is the "?" symbol in URL used for in php?
(7 answers)
Closed 9 years ago.
Sorry if this is a stupid question but I want to include two variables in an href
the href is not in a form
<div class="button">
View
</div>
int he page I am sending it to i have a Get method
if (isset($_GET['Name']) && isset($_GET['pid'])) {
$id2 = $_GET['Name'];
$id = $_GET['pid'];
Summary
I want to include $id in the a href so i will be able to get both name and id in the page product.php
You can append as many URL parameters as you like using &:
<a href="<?php fetchdir($ipages); ?>product.php?Name=<?php echo $name; ?>&id=<?php echo $id; ?>" ...
product.php?Name=$name&pid=$pid
You can use & to add two or more GET parameters to a URL.
One good way to build a property encoded is to use the http_build_query function, this will ensure that the data sent in the request is properly encoded.
<?php
$qstr = http_build_query(array("Name"=>$row["Name"], "id"=>$row["pid"]));
?>
<a href="<?php fetchdir($ipages) ?>product.php?<?= $qstr ?>">
This of course can also be done inline
<a href="<?php fetchdir($ipages) ?>product.php?<?= http_build_query(array("Name"=>$row["Namw"], "id"=>$row["pid"])) ?>">
Related
This question already has answers here:
get file name for url php
(2 answers)
Closed 3 years ago.
I want to separate IMDb id from the link
my php code is this
<?php print $movie_info[0]->imdb_link; ?>
the URL seems like this on every page .
https://imdb.com/title/tt1206885
i want to show this id separate from the url on my page => tt1206885
please help me
i try but fail to extract the id
<?php if($movie_info[0]->imdb_link){?>
ID <?php $str = explode("/",$movie_info[0]->imdb_link);
/* if url in php variable then $str = explode("/",$imdb_id); */
print $str[count($str)-2];
?>">
<?php }?>
$link_array = explode('/', $movie_info[0]->imdb_link);
$id = end($link_array);
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
How can I combine two strings together in PHP?
(19 answers)
Closed 4 years ago.
I have a problem because I cannot call my data that I want to retrieve on a video src and image src. here is my code. image and video src are not showing.
can someone give me the correct syntax, please? thanks
few questions, do I need to concatenate something? do my rows[''] are concatenate correctly?
but my main problem here is I cannot click or change any videos that are retrieved or stored. how can I fix the onClick? thanks
<?php
echo '<li>
<a href="javascript:void();" onClick="document.getElementById("vid_frame").src="images/promvid/pal/<?php row['videos'] ?>">
<span class="vid-thumb">
<img width=72 src="images/promvid/philippines.jpg"/<?php row['image'] ?>
</span>
<div class="desc">Philippines<?php row['title'] ?>
</div></a></li>';
?>
MY ERROR
Parse error: syntax error, unexpected 'videos' (T_STRING), expecting
',' or ';
You cannot use PHP tags inside PHP tags you need to concatenate the string part with variable using echo.
<?php
echo '<li>
<a href="javascript:void();" onClick="document.getElementById("vid_frame").src="images/promvid/pal/'.$row['videos'].'">
<span class="vid-thumb">
<img width=72 src="images/promvid/philippines.jpg"/'.$row['image'].' </span>
<div class="desc">Philippines'.$row['title'].' </div></a></li>';
?>
This below code may help you.
In php we need use the variable by using $
echo '<li>
<a href="javascript:void();"
onClick="document.getElementById("vid_frame").src="images/promvid/pal/"'.$row["videos"].'>
<span class="vid-thumb">
<img width=72 src="images/promvid/philippines.jpg/"'.$row['image'].'>
</span>
<div class="desc">Philippines'.$row['title'].'</div>
</a>
</li>';
You are using bad practice and run into errors. You cannot have <?php ?> tags within <?php ?> tags.
Write clean code and make good practice to a habit.
PHP is an embedded language. Do not try to generate all HTML by PHP echo. Embed peaces of PHP code into a surrounding HTML template. You can even close PHP tags within a block of loop constructs.
Variables standing alone in a PHP tag are not output, except you are using shot-open tags <?=$variable?>. Short-open tags should not be used since most server configurations do not enable them.
Inline JavaScript is old school and considered to be bad practice. First of all inline on-handlers might be removed from the standards some day. Use event listeners instead.
You have block elements div inside the inline element a. This is valid only in HTML5. Ensure that your document type explicitly is HTML5 by prepending <!DOCTYPE html> as the very first line in the document.
Links doing nothing has been widely discussed if the href attribute should be # or javascript:void(0);. I tend to the latter. void is a keyword, however, the function style is fine. Regardless to the code style there has to be an argument.
even better: Do not use non-linking links at all. Use links with a valid fallback URL instead. In the event handler you can call the preventDefault() method wich will prevent the href action, i.e. location change.
PHP site:
<head>
<title>Non-Empty Title</title>
<body>
<ul>
<?php
// fake query result
$rows =
[
[
'videos' => 'phillippines.mpeg4',
'image' => 'philippines.jpg' ,
'title' => 'Philippines' ,
'noscript-frame-page' => 'philippines.html',
],
[
'videos' => 'usa.mpeg4',
'image' => 'usa.jpg' ,
'title' => 'USA' ,
'noscript-frame-page' => 'usa.html',
]
];
// fake fetch row
foreach ($rows as $row)
{
?>
<li>
<a class="video-ref"
href="<?php echo $row['noscript-frame-page'];?>"
data-video-src="images/promvid/pal/<?php echo $row['videos'];?>"
target="video-frame"
>
<span class="vid-thumb">
<img width=72 src="images/promvid/"<?php echo $row['image'];?>
</span>
<div class="desc"><?php echo $row['title'];?>
</div>
</a>
</li>
<?php
}
?>
</ul>
<noscript>
<iframe id="video-frame" name="video-frame" src="start-video-page.html"></iframe>
</noscript>
<div id="debug">DEBUG OUTPUT</div>
<script src="my-script.js"></script>
my-script.js
document.addEventListener('DOMContentLoaded', evt =>
{
"use strict";
const VideoLinkListener = evt =>
{
const videoSrc = evt.currentTarget.getAttribute('data-video-src');
if(videoSrc)
{
document.getElementById('debug').innerHTML = videoSrc; // document.getElementById('vid_frame').src=videoSrc;
// do not change location to link's href
evt.preventDefault();
}
};
document.querySelectorAll('a.video-ref').forEach( link => link.addEventListener('click', VideoLinkListener) );
});
This question already has answers here:
How to add url parameter to the current url?
(7 answers)
Closed 7 years ago.
I have an search method who redirect to something like this:
mysite.ro/search.php?a=1&b=2&c=3
When i have pagination i need to redirect me to something like this:
mysite.ro/search.php?a=1&b=2&c=3&page=2
I dont know the solution to have an href who redirect to page=2 and keep the same search criteria.
I tried <a href="&page=2"> and href="<?php echo $_SERVER[REQUEST_URI];?>&page=1" not usefull both.
Try this, Its worked for me.
<?php
$actual_link = '';
$delimiter = '?&page=';
$link = array();
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$links = explode($delimiter, $actual_link);
?>
<a href="<?php echo $links[0];?>?&page=1" >Link </a>
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
Hi I am trying to produce a <td> which also contains an <a> link which will redirect to a function in my controller which also echos the id of my data. Here is my code so far:
<?php
if ($this->session->userdata("username")==$info->U_username) {
echo '<td>EDIT</td>';
}
?>
This code produces an error Disallowed Key characters. Any help or comment is highly appreciated.
For concatenating strings PHP has . operator.
echo '<td>EDIT</td>';
You need to add the result of base_url() to the string you want to output, e.g.:
echo '<td>EDIT</td>';
Either you need to concatinate instead to use php multiple time .use like this
<?php
if ($this->session->userdata("username")==$info->U_username){
echo "<td><a href='".base_url()."'/gamestalker/edit_content/'".$info->C_id."'>EDIT</a></td>";
}
?>
or don't include html into php tags like this
<?php
if ($this->session->userdata("username")==$info->U_username){ ?
<td>EDIT</td>
<? }
?>
You need string concatenation. In PHP you use . for that.
Also codeigniter's base_url can take an argument:
<?php
if ($this->session->userdata("username")==$info->U_username){
echo '<td>EDIT</td>';
}
?>
This question already has answers here:
Question mark in the middle of a URL variable
(3 answers)
Closed 8 years ago.
i want to pass value that contain question mark
i have 2 php example example1.php and example2.php
in my example1.php the code like this
<a href="example2.php?title=what is this?" />
in my example2.php the code like this
<php
if(isset($_GET['title']))
{
$title = $_GET['title'];
echo $title;
}
?>
but in example2.php the title become like this what is this the question mark disappear
i already try to use str_replace my question mark become like this what is this"?" but still same at example2.php the question mark disappear
please help me how to pass value that contain question mark
thanks
change <a href="example2.php?title=what is this?" /> into
<a href="example2.php?title=what is this%3F" />
then add
<?php
if(isset($_GET['title']))
{
$title = $_GET['title'];
echo $title;
}
?>
this will work for you.