I'm having this problem
Uncaught SyntaxError: Unexpected end of input
in this line
<?php ... foreach($res as $row) {echo '<input onclick="selectall('.$j.',"flow'.$row['uid'].'","hi'.$row['uid'].'")" type="submit" class="btn btn-primary btn-user btn-block" value="Update" />';} ?>
Output of that line
<input onclick="selectall(5,"flow9C2748C40A24","hi9C2748C40A24")" type="submit" class="btn btn-primary btn-user btn-block" value="Update" />
The problem is here i think
,"flow'.$row['uid'].'","hi'.$row['uid'].'"
because when i remove it, the problem disappear
Appreciate any help !
There are a couple of functions which should possibly help when it comes to formatting strings - namely printf and sprintf (others in this family also exist) - and these allow you to specify placeholders in the string which are substituted with the arguments provided. Using these helps simplify how the strings are escaped
printf(
'<input type="submit" onclick="selectall( \'%1$s\', \'flow%2$s\', \'hi%2$s\' )" value="Update" class="btn btn-primary btn-user btn-block" />',
$j,
$row['uid']
);
Related
return '<form action="'.route('own_product_delete', $product_datatable['id']).'" method="post"><input type="hidden" name="_method" value="delete" /><input type="hidden" name="_token" value="'.csrf_token() .'"><button type="submit" onclick="return confirm()" class="btn btn-danger btn-sm mt-1"><i class="fa fa-trash"></i></button></form>';
I have used single quote for php and double quote for html but how to write inside return confirm()? I mean I need to write return confirm('Are you Sure>')
complexity is, it's inside single quote for php and then double quote for html 'onclick="return confirm()"' I am not able to use any quote for confirm message. Please suggest me
I have this button:
<button type="button" class="btn btn-danger" id="deleteuserbtn" value="${usr.idUser}" onclick="deleteUser(this.value)">Delete</button>
That calls the function deleteUser once the button is clicked.
I would like to know how to send more than one value at the same time.
For example, if I want to send the idUser and the userName, how can I do it?.
I tried this but it's not working:
<button type="button" class="btn btn-danger" id="deleteuserbtn" value="${usr.idUser, usr.userName}" onclick="deleteUser(this.value)">Delete</button>
I expect to receive the values in the same jsp page:
function deleteUser(val1, val2) {
alert(val1);
alert(val2);
}
You can simply pass your values under your function under '' quotes else it will give you error not defined.So your button code will look like below :
<button type="button" class="btn btn-danger" value="something" id="deleteuserbtn" onclick="deleteUser(this.value ,'${usr.idUser}','${usr.userName}')">
Delete</button>
Demo Code :
function deleteUser(val1,val2,val3){
alert("val :"+val1 +" "+val2+" "+val3);
}
<button type="button" class="btn btn-danger" id="deleteuserbtn" value="something" onclick="deleteUser(this.value,'${usr.idUser}','${usr.userName}')">
Delete</button>
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'
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>";
?>
I'm trying pass data to jquery via onclick
There is jquery function:
function handleit(cId, anotherId) {
var campaingID = cId;
var another = anotherId;
var post = "action=remove&campaign_id=" + campaingID + "&anotherid=" + another;
$.ajax({
type: "post",
url: "ajax/phphandler.php",
data: post,
cache: false,
success: function(html) {
$('#ready').html(html);
}
});
return false;
}
and there is onclick code
<td><input class="btn btn-default" type="submit" data='.$row2['campaign_id'].' value="submit" onclick="return handleit("'.$row2['campaign_id'].'","test")"></td>
rendered html
<td><input class="btn btn-default" type="submit" data=43 value="submit" onclick="return handleit(" 43","test")"></td>
The issue is that your HTML winds up looking like:
<input class="btn btn-default" type="submit" data=foo value="submit"
onclick="return handleit("campaign_id","test")">
^ ^ ^ ^
When defining an attribute, you can't nest " inside ". The HTML would need to look like:
<input class="btn btn-default" type="submit" data=foo value="submit"
onclick="return handleit('campaign_id','test')">
^ ^ ^ ^
How you accomplish that depends on whatever server-side language you're using, but probably involves escaping quotation marks:
<td>
<input class="btn btn-default" type="submit"
data="'.$row2['campaign_id'].'" value="submit"
onclick="return handleit(\''.$row2['campaign_id'].'\',\'test\')"
</td>