onclick function using php - php

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>

Related

Not able to access appended variable in URL in PHP

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

Add PHP variable inside echo statement as href link address?

I'm trying to use a PHP variable to add a href value for a link in an echo statement.
Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.
$link_address = '#';
echo 'Link';
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link
you can either use
echo 'Link';
or
echo "Link';
if you use double quotes you can insert the variable into the string and it will be parsed.
Basically like this,
<?php
$link = ""; // Link goes here!
print "Link";
?>
as simple as that: echo 'Link';
You can use one and more echo statement inside href
Link
link : "/profile.php?usr=firstname&email=email"
This worked much better in my case.
HTML in PHP: Link
The safest way to generate links in PHP is to use the built-in function http_build_query(). This function is very easy to use and takes an array as an argument.
To create a dynamic link simply echo out the result of http_build_query() like so:
$data = [
'id' => $id,
'name' => $name
];
echo 'Link';
If you want to print in the tabular form with, then you can use this:
echo "<tr> <td><h3> ".$cat['id']."</h3></td><td><h3> ".$cat['title']."<h3></</td><td> <h3>".$cat['desc']."</h3></td><td><h3> ".$cat['process']."%"."<a href='taskUpdate.php' >Update</a>"."</h3></td></tr>" ;

jQuery string is not defined

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.

echo a javascript function with parameters in php

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

Passing variable through function Javascript / PHP

i want to pass a variable through to a php on an onClick event, it doesn't seem to be passing it through to the page properly though.
Here's my html
<a href="data.php" onClick="video(We Have a Pope)" hidefocus="">
and the relevant javascript function
function video(result) {
$('#information').load('data.php?result=' + result);
document
}
My php page looks like this
<?php
$result = $_GET['result'];
echo $result;
?>
Nothing is being outputted though :S
onClick="video(We Have a Pope)" should be onClick="video('We Have a Pope')"
You need quotation marks when you pass string as parameter, like this:
<a href="data.php" onClick="video('We Have a Pope')" hidefocus="">
<a href="data.php" onClick="javascript:video('We Have a Pope');" hidefocus="">
onClick="video('We Have a Pope');"
or
onClick="video(\"We Have a Pope\");"

Categories