Pass ID on onClick Function in Javascript - javascript

I am trying to pass the email using the onClick function and I want to show that email in alert:
<td>
<button class="btn btn-primary" name="buton" onclick="myfunction(<?php $email; ?>)" >Edit</button>
</td>
Here is my function:
<script type="text/javascript">
function myfunction(id)
{
alert(id);
}
</script>

You've to make two changes to validate your code :
You should add echo to pass the php variable to JS.
You need to add single quotes since the $email is a string and you've already using double quotes in onclick attribute.
<td>
<button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button>
</td>

try this
when you are passing the email(String) it should be in quote and while using php in javascript in this case u have to use echo
<td><button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button></td>

What you want is probably the PHP tag <?= that echoes the $email variable as an argument to the JavaScript function. Note that you will need quotation marks for the email:
onclick='myfunction("<?= $email ?>")'
If shorttags are not enabled the long version must be used:
onclick='myfunction("<?php echo $email; ?>")'

echo the variable in the php tag
<td><button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button></td>

Your code is working, you are just not echoing the value of $email in a good way:
- Lack of the single quotes in myfunction(' … ')
- Lack of echo inside your php tags myfunction('<?php echo $email ?>')
To avoid that kind of problems…
In your php, I suggest you to use heredoc syntax:
echo <<< EOD
<td><button class="btn btn-primary" name="buton" onclick="myfunction('{$email}')">Edit</button></td>
EOD;
Note the { } around the variable. They are not necessary, but I suggest you to use them, as they make the variables more noticable.
⋅ ⋅ ⋅
The result will be:
function myfunction(id) {
alert(id);
}
<td><button class="btn btn-primary" name="buton" onclick="myfunction('myemail#stackoverflow.com')">Edit</button></td>
Documentation about Heredoc syntax: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
I hope it helps.

function myfunction(id)
{
alert(id);
}
<td>
<button class="btn btn-primary" name="buton" onClick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
You need to put the email in 'single quotes'

Related

jQuery add button attribute using closest method

Here is the button for adding items to cart
<div class="title_6">
<button class="btn my-cart-btn bt_cart btn-large btn-positive"
data-id="<?php echo $row['product_id'];?>"
data-name="<?php echo $row['product_name'];?>"
data-summary="<?php echo $row['pres_name'];?>"
data-price="<?php echo $row['sales_price'];?>"
data-quantity="1"
data-image="admin/upload/<?php echo $row['photo_1'];?>"
>Add to Cart</button>
</div>
I'm trying to set data-quantity attribute of button using jQuery. As there are multiple items I'm using closest method. What I've tried-
$(this).closest('.title_6').find('button').attr("data-quantity", qty);
But its not working. How to do that properly ?
Update:
Here is the code for (+) button. What is tying to do, When (+) button .bt_3 is clicked I'll add attribute to another button "Add Cart"
$(document).ready(function() {
$('.product_row').on('click', '.bt_3', function() {
var qty = $(this).closest('.title_5').find('input').val();
var qty_add = Number(qty)+Number(1);
$(this).closest('.title_5').find('input').val(qty_add);
$(this).closest('.title_6').find('button').attr("data-quantity", qty_add);
});
});
HTML for (+) & (-) button along with Add Cart Button for better understanding
<div class="title_5">
<button class="btn bt_1 btn-large btn-negative">-</button>
<input type="text" class="bt_2" value="1">
<button class="btn bt_3 btn-large btn-primary">+</button>
</div>
<div class="title_6">
<button class="btn my-cart-btn bt_cart btn-large btn-positive" data-id="<?php echo $row['product_id'];?>" data-name="<?php echo $row['product_name'];?>" data-summary="<?php echo $row['pres_name'];?>" data-price="<?php echo $row['sales_price'];?>" data-quantity="1" data-image="admin/upload/<?php echo $row['photo_1'];?>" >Add to Cart</button>
</div>
Try changing this line:
$(this).closest('.title_6').find('button').attr("data-quantity", qty_add);
for this one:
$(this).parent().next('.title_6').find('button').attr("data-quantity", qty_add);

Quote is breaking the syntax even after escape

I have a button who should call an action with 2 parameters (an integer and a String). I m using Yii2.
<button class="button_answer"
onclick="submitAnswer(
<?php echo $id ?>,
<?php echo '\''.Html::encode($title).'\''?>
);">
Submit your answer
</button>
it is working, but when the parameter title contains a single quote or a double quote, the syntax is broken.
I become something like this:
<button class="button_answer" onclick="submitAnswer(214, 'What's the ...?');">
Post your answer
</button>
I dont know how to solve this.
You need to encode the PHP string for JavaScript. Then you need to encode the JavaScript for HTML.
<?php
$js_string_title = json_encode($title);
$js = "submitAnswer($id, $js_string_title)";
$html_safe_js = htmlspecialchars($js);
?>
<button class="button_answer"
onclick="<?php echo $html_safe_js; ?>">
Submit your answer
</button>
A nicer approach would be to avoid inlining the JS altogether:
<button class="button_answer"
data-id="<?php echo htmlspecialchars($id); ?>"
data-title="<?php echo htmlspecialchars($title); ?>">
Post your answer
</button>
Along with something like:
addEventListener("click", answer_handler);
function answer_handler(event) {
var el = event.target;
if (el.classList.contains("button_answer")) {
submitAnswer(el.dataset.id, el.dataset.title);
}
}

Trying to pass multiple parameters from php to javascript

Heres my current code in php with one parameter which works
<?php
$sid = "012";
echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.')">';
?>
Now im trying to pass two parameters or more, but cant seem to get it to work, here is my attempt:
<?php
$name="abc
$sid = "012";
echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.','.$name.')">';
?>
You need quotes around the parameters or javascript treats them like variables instead of strings.
echo '<input type="button" value="Submit" onclick="changeConfirmed(\''.$sid.'\',\''.$name.'\')">';
If one of your JavaScript function parameters is a string, you'll have to correctly enclose it in quotes:
<?php
$name="abc";
$sid = "012";
echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.',\''.$name.'\')">';
?>
You get away with $sid not enclosed in quotes because it looks like a number.

onclick location href not working with php variable

I have a button with a link that involves a PHP variable, but when the button is clicked, nothing happens and I am not brought to a new page according to the link. I tested the link with a <a> tag instead of a <button> tag, and the link works. I am new to PHP so any suggestions are appreciated.
Here is the code:
<?php
echo
'<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick="location.href="' . $child_page . '"">
KEEP READING <span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
echo 'Link ';
?>
The variable can look like: /n/2/ar/ and will apply like this: www.example.com/n/2/ar/. Please note that the inline CSS is only for testing purposes.
You have a problem with the quotes
Try this:
<?php
$x = 'http://test.com';
$y = 'location.href="'.$x.'"';
echo
'<button type="button" class="btn btn-secondary"
id="btn"
style="margin-left:0%;color:gray"
onclick='.$y.'>
KEEP READING
<span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
?>
There is a problem with the quotes..Nothing else
Use this:
echo
'<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick=location.href="'.$child_page.'">
KEEP READING <span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
It looks like you have a problem with your opening tag. The code
$child_page = '/n/2/ar/';
echo '<button ... onclick="location.href="' . $child_page . '"">';
renders to
<button ... onclick="location.href="/n/2/ar/"">
which is understood by browser as
<button ... onclick="location.href=" /n/2/ar/ "">
So the location is being empty.
Try wrapping url into single quotes instead:
$child_page = '/n/2/ar/';
echo '<button ... onclick="location.href=\'' . $child_page . '\'">';
If you begin with double-quotes instead of single-quotes, it can be done this way:
$child_page = 'this.html';
echo "<button type='button' class='btn btn-secondary' id='btn' style='margin-left:0%;color:gray' onclick=\"location.href='$child_page'\">
KEEP READING <span class='glyphicon glyphicon-chevron-right' style='color:gray'></span>
</button>";
echo "<a href='$child_page'>Link </a>";
Double-quotes allow you to place a variable 'as-is' whereas with single-quotes you'll have to do a couple extra steps.
Try this
<?php
$child_page = 'https://www.google.com';
echo '<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick="location.href=\'' . $child_page . '\'">
KEEP READING <span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
echo "<a href='$child_page'>Link </a>";
?>

Two onclick on one element

I have a simple code to delete data from the database,and I want a warning message to appear before deleting. the code works fine when I put one onclick and when I put two only the first onclick works.
and I need to onclick one for the warning message and the other for delet.php page.
<form><input type="button" value="delete" onclick="return confirm('Really delete?');" onClick='window.location.href="delete.php?id= <?php echo $id; ?>"' ></form>
This is how I would handle it:
<input type="button" value="delete"
onclick="confirmDelete('<?php echo $id; ?>');">
Then elsewhere in the page:
<script type="text/javascript">
function confirmDelete(id) {
if(confirm('Really delete?')) {
window.location.href= "delete.php?id=" + id;
}
}
</script>
Notice I removed the <form> element. If this is the only context this is in, the <form> element is unnecessary since you're not actually filling out a form, just clicking a button. If there is more context you didn't include in your question, then it might be relevant to that.
Try using this with an if clause:
<form>
<input type="button" value="delete" onclick="
if(confirm('Really delete?')) {
window.location.href='delete.php?id=<?php echo $id; ?>';
}
else {
return false;
}
">
</form>
You can only use one onClick. But there's a way to do what you want in only one ;)
<form>
<input type="button" value="delete" onclick="
if(confirm('Really delete?')){
window.location.href=\"delete.php?id= <?php echo $id; ?>\"
}else{
return false;
}" />
</form>

Categories

Resources