<?php $text="Alice's";?>
($text data comes form database, I just put an example like Alice's)
<button type="submit" onclick="Remove('<?php echo $text; ?>')"></button>
<script>
Remove(text){
alert(text);
}
</script>
When I use ' at $text variable Javascript function Remove() does not work. I use php htmlentities() function but it does not work
This is because once your string is passed into the onClick function it is getting confused with all the ' and ".
So to make this work you could escape your variable ' with a \
Depending where the data is coming from you may need to write something that escapes these automatically for you.
<?php $text="Alice\'s"; ?>
<button type="submit" onclick="Remove('<?php echo $text; ?>')"></button>
<script>
function Remove(text){
alert(text);
}
</script>
Related
I am trying to call a function in javascript by clicking a button but the problem is that when I used to click the button then it do nothing i.e it doesn't call the function...
The following is the code
<?php
function view_cat1()
{
$i=1;
include("inc/db.php");
$fetch_data=$con->prepare("select * from main_cat order by cat_name");
$fetch_data->setFetchMode(PDO::FETCH_ASSOC);
$fetch_data->execute();
while($row=$fetch_data->fetch())
{
echo"<tr><td>".$i++."</td>
<td>".$row['cat_id']."</td>
<td>".$row['cat_name']."</td>
<td><a href='index.php?edit_cat=".$row['cat_id']."'>Edit</td>
<td><input type='button' onclick='delete_category(<?php echo
".$row['cat_id'].")' name='delete' value='Delete'></td>
</tr> ";
}
}
?>
<script language="javascript'">;
function delete_category(delname)
{
if(confirm("are you sure"))
{
window.location.href='delete_cat.php?del_name='+delname+'';
return true;
}
</script>
<?php
The above code don't use to show the confirmation message .
Modify the JavaScript as below, it may works:
<script>
function delete_category(delname)
{
if(confirm("are you sure"))
{
window.location.href='delete_cat.php?del_name='+delname+'';
return true;
}
}
</script>
The language attribute has been deprecated for a long time, and should not be used. Also, you missed the closing brace } of the delete_category function.
<script>
function delete_category(delname) {
if (confirm("are you sure")) {
window.location.href = 'delete_cat.php?del_name=' + delname + '';
return true;
}
}
</script>
You can use the attribute type="text/javascript" if you are not using HTML5
To simplify your code and make fewer mistakes, you could use a single echo statement in your while loop. If you render HTML with JavaScript functions linked with events, don't forget to add quotation marks around the arguments of the function. In your case, the function delete_category takes in as argument $row['cat_id']. To do that escape the double quotes with a blackslash : \"
Here is an example:
<?php
$var = "hello";
echo "<input type='button' onclick='delete_category(\"".$var."\")' name='delete' value='Delete'>";
I have searched and tried several different methods to pass a php $variable to a jQuery function. I can pass a string simply by using myfunction("Hello");. But if I try myfunction(); or myfunction($variable); with or without quotes it fails to run.
function wholesection(val) {
$("#whole-section").slideUp("fast", function () {
});
$('#label-cemetery').text("Section*");
$('#poc').val(val);
}
The above works if I send a literal string enclosed in double quotes, using:
<?php
echo '<script>',
'wholesection("Hello");',
'</script>'
;
?>
</head>
<body>
<?php
$variable = "Hello";
echo '<script>',
'wholesection(' . $variable . ');',
'</script>'
;
?>
Or other similar variants do not work.
'wholesection($variable);',
'wholesection("$variable");',
Suppose your $variable has value "Hello".
Then this code:
echo 'wholesection('.$variable.');',
is rendrered in html like
wholesection(Hello);
See? You're passing Hello to a function. Not "Hello" but Hello.
And Hello is considered a javascript variable. I bet you don't have it.
So, the fix is - add quotes:
echo 'wholesection("'.$variable.'");',
which will be rendered as:
wholesection("Hello");
You can pass it by echoing your php varriable in the script
try below code :
<?php
$variable = "Hello";
?>
<script type="text/javascript">
var simple = '<?php echo $variable; ?>';
</script>
<?php
$variable = "Hello";
if(true){
?>
<script>
wholesection("<?php echo $variable?>");
</script>
<?
}
?>
i'm using PHP to get some values from the Database & echo html code. So i have something like this..
$values = $params->get('something');
And then i want to pass $values in to the doSomething() function of javascript.
echo('<html>');
echo('<head>');
echo('</head>');
echo('<body>');
echo('<button type="button" onclick="doSomething()">');
echo('</button>');
echo('</br>');
echo('</body>');
echo('</html>');
Any suggestions? :D
You can do this -
echo('<button type="button" onclick="doSomething(\'' . $values . '\')">');
html
<?php $test="00010387";
javascript
<script>
function browsePricebySKU(test){
alert(test);}
</script>
the output is different with the parameter. Please help me.
Try this
php code
<?php $test="00010387"; ?>
<script type="text/javascript">
function browsePricebySKU(test){
alert(test);
}
</script>
Single quotes will not be Escape, It will output $test as a string, not the value of the $test variable;
html
<?php $test="00010387";?>
(close php before starting html)
or
html
<?php $test="00010387";
echo "";
I'm trying to auto click link which action is a javascript function. But it won't work..
if($submit)
{
echo '<body onload="setTimeout("autoClick();",1000);">';
echo "<a id='linkToClick' onclick='return confirmDialog($id);'>clickme</a>";
echo '</body>';
}
else
echo 'not set';
?>
<script type="text/javascript">
function autoClick(){
var myLink = document.getElementById('linkToClick');
myLink.click();
}
function confirmDialog (id, callback) {
confirmDialogCallback = callback;
$("#idConfirmDialog").modal ("show");
}
</script>
One problem here is that you're using double quotes as both your attribute delimeter and around the parameter you are passing to setTimeout (note how the syntax highlighting gets all messed up here):
<body onload="setTimeout("autoClick();",1000);">
should be:
<body onload="setTimeout('autoClick();',1000);">
And in your PHP code, this would equate to:
echo '<body onload="setTimeout(\'autoClick();\',1000);">';
Also, while this is not strictly necessary, passing a string value to setTimeout is not a recommended practice and should be avoided. You should pass the function itself instead:
<body onload="setTimeout(autoClick, 1000);">
And if you do that, you have one fewer pair of quotes to worry about.