I am trying to pass a variable from one page to another by appending it to the URL like this-
<a href='single.php?name=".$m['Title']." ' class='movie-beta__link'>
and in the next page(single.php), I am getting the url as-
$title= $_GET['name'];
echo $title;
But nothing is displayed and my URL looks like this-
http://localhost/mdb/single.php?name=
Can someone please point out if I am missing something here.
Try this
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
You're getting your single and double quotes mixed up. With single quotes you have to append variables and with double quotes you can put them inside. But when you're outside of <?php tags you can use <?= to echo a variable.
<a href="single.php?name=<?= $m['Title'] ?>" class="movie-beta__line">
This will work above:
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
if all this is in php echo, use <a href='single.php?name='".$m['Title']."' class='movie-beta__link'>Title</a>
And its good to avoid underscores on you class names
Related
My onclick function is not working.It is not passing the value (parameter)?
<img class="img-thumbnail thumbnails" src="download.jpg" alt="bridget_moynahan_00.jpg" title="bridget_moynahan_00.jpg" onclick="showImage(<?php echo "download.jpg";?>);" />
Your code is producing the following JavaScript:
showImage(download.jpg);
In JavaScript, as in other languages, string literals need to be surrounded by quotes. For example:
showImage('download.jpg');
One way to do that here would be like this:
showImage('<?php echo "download.jpg";?>');
Or possibly:
showImage(<?php echo "'download.jpg'";?>);
Try like this
onclick="showImage('<?php echo "download.jpg"; ?>')";
Try this:
onclick="showImage(<?php echo "'download.jpg'";?>);"
Also make sure that you have define the function showImage or that you have add the script file like:
<script src="/js/custom_filename.js"></script>
I am trying to display a PHP variable within a <p> tag but I either keep getting errors or the name of the variable is shown but not it's content.
Here is the code:
echo '<div class="item" style="background-color:'.$row[colour].';width:'.$row[width].'px;"><p>$row[name]</p></div>';
Where I have written:
<p>$row[name]</p>
will not show up in the browser correctly and instead will just show the name of the variable. If I surround the variable with '' I get an error regaurding syntax.
I have also tried to echo the variable:
<p><?php echo $row[name] ?></p>
But on the website nothing is displayed at all and when I look in the FireFox inspector I see this:
The code has automatically been commented out?
You need to add '. $row[name] . '
like this
echo '<div class="item" style="background-color:'.$row[colour].';width:'.$row[width].'px;"><p>' . $row[name]. '</p></div>';
You have to concatenate the php variable.
Replace
echo '<div class="item" style="background-color:'.$row[colour].
';width:'.$row[width].'px;"><a href="'.$row[link_url].
'" title="'.$row[name].'"><p>$row[name]</p></a></div>';
with
echo '<div class="item" style="background-color:'.$row[colour].
';width:'.$row[width].'px;"><a href="'.$row[link_url].
'" title="'.$row[name].'"><p>'.$row[name].'</p></a></div>';
Notice '"><p>'.$row[name].'</p></a></div>';
When interpolating array values in strings you need to use curly brackets and double quotes.
echo ...."\"><p>{$row['name']}</p></a></div>";
When you use single quotes, php does not interpolate variable names inside the string. Instead, it just prints the names, which is what you're seeing in the source code.
And your array keys should be strings, so ['name'] instead of [name]
Not sure if this is 100% as I am half asleep but at least it will help you with your array a bit.
echo "<div class=\"item\" style=\"background-color:".$row['colour'].";width:".$row['width']."px;\"><p>".$row['name']."</p></div>";
I have a HTML achor tag like below:
echo '<a href="javascript:tempBuy('.$res_get_price[0][0].','.$res_get_price[0][1].','.$res_get_price[0][2].','.$dt_str.')">'.$res_get_price[0][0];
And the corresponding javascript function tempBuy() is
function tempBuy(rate,veg_name,market_name,dt)
{
alert(dt);
}
But the problem is it does not alert at all ! May be I need to include the variable names within single quotes in tempBuy() function. I tried tempBuy(\'var1'\,\'var2\'...) but it shows error. How can I able to to that. Thanks .
Source for the part shows like this:
<td width="120px" class="">56.0
</td>
<script>
function tempBuy(rate,veg_name,market_name,dt)
{
alert(rate);
}
</script>
You didn't wrap your javascript arguments in quotes. You need to wrap each variable in single quotes, since you used double quotes for "href" attribute. Another thing is that you didn't close up "a" HTML tag.
echo ''.$res_get_price[0][0].'';
If there is anything in your variables that is not a valid javascript literal you have to make it a string like:
echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\' ...
If there are ' in your variables you have to replace them with \' as well.
As you can see form the rendered output, you need to quote the last 3 arguments which are non-numeric. The correct output should be: javascript:tempBuy(56.0,'Apple','Bangalore','2013-05-18')
The corrected PHP code is:
echo ''.$res_get_price[0][0].'';`
echo "<a href=\"javascript:tempBuy('".$res_get_price[0][0]."','".$res_get_price[0][1]."','".$res_get_price[0][2]."','".$dt_str."')\">".$res_get_price[0][0];
I'm sending data to function by onclick event but I can't get string value I just getting integer value, it say that 'value' is not defined. what is the problem.
My code is:
<a href="javascript:void(0)"
onclick="begin(<?php echo $data['user_id'];?>,
<?php echo $data['name'];?>);">
This is my function:
function begin(id,name)
{
alert(id);
alert(name);
}
I'm not getting name value, if I pass hard-code string then its also not getting here only integer are accessible.
You need to wrap your parameters in quotes to make it a string.
<a href="javascript:void(0)" onclick="begin('<?php echo $data['user_id'];?>','<?php echo $data['name'];?>');">
As Matt says, without quotes it won't be recognised.
That said, I don't think his answer is correct. I would prefer this code: (whitespace added for legibility)
<a href="javascript:void(0);" onclick="begin(
<?php echo htmlspecialchars(json_encode($data['user_id'])); ?>,
<?php echo htmlspecialchars(json_encode($data['name'])); ?>
);">
json_encode (docs) is good for passing any PHP variable (except Resources) into JavaScript. In this case, it will add quotes around the string, and escape characters as needed with backslashes. Since it's going in an attribute, you need htmlspecialchars to convert symbols to be safely insertable.
I am trying to pass a JavaScript function with an onclick event in php. The problem I am facing is that the function that I need to pass has a parameter that needs to be in double quotes as follows:
onclick="removeElement("div8")"
Now when I use JavaScript to generate the parameter it comes out fine, but whenever I use an echo function in php, the following happens when I look at the function in the browser
onclick="removeElement(" div8")"
the code I am using to generate this is:
echo '<div><img src="img.png" alt="image" onclick="removeElement("div'.$x.'")" /></div>';
where $x is the number to be added to the parameter.
Is there a way that the function is returned as a whole and not get the space in between?
This is happening because you have quotes inside quotes. This will not work, and breaks the HTML parser. It is seeing the onclick as removeElement(, and then it sees an attribute called div8")".
Try this:
echo '.....onclick="removeElement("div'.$x.'")"...';
HTML entities are parsed inside attributes, so the result will be your working code.
Change your echo to this:
echo '<div><img src="img.png" alt="image" onclick="removeElement(\'div'.$x.'\')" /></div>';
You must escape quotes in javascript. Instead of
onclick="removeElement("div8")"
you should write
onclick="removeElement("div8")"
try escaping your single-quotes
echo '<div><img src="img.png" alt="image" onclick="removeElement(\'div'.$x.'\')" /></div>';
As long as you do not use any spaces in the attribute value you can ommit the quotes around the html attribute values. All browsers will handle that fine. So you can write:
onclick=removeElement("div8")
You could also use the single quotes:
onclick="removeElement('div8')" or
onclick='removeElement("div8")'
Or you can escape the double quote:
echo '<div><img src="img.png" alt="image" onclick="removeElement(\"div'.$x.'\")" /></div>';
But a simpler solution would be to write in html directly:
?>
<div>
<img src='img.png' alt='image' onclick='removeElement("div<?php echo $x; ?>")' />
</div>
when you using php echo try this code
<a href="javascript:add_cota(<?php echo $value->ID .','.$k.', \''.$st.'\'';?>)">
$st
is the string param to avoid the ReferenceError: Active is not defined error