I got a Javascript function called edit like this :
function edit(id){
window.opener.location.href='../../../index.php?mi=<?php echo $mi1?>&id='+id;
//window.close();
}
It already work if i use a href so i need to click the link
<a href="javascript:void(0);" onClick="edit('<?php echo $_POST["id"]?>');">
<?php echo $_POST["id"]?>
</a>
Then i want to run the function when i save / update data
if(isset($_POST["save"])){
...
...
echo " <script>$(document).ready(function(){edit(".$_POST["id"].")};</script>";
}
But it didn't work, did i do something wrong ?
try this
$id = $_POST["id"];
echo "<script>$(document).ready(function(){edit(".$id.")});</script>";
you have used double quotation like this edit(".$_POST["id"].").
also closing ) is missing.
Try This Code
if(isset($_POST["save"])){
echo "<script>$(document).ready(function(){edit(".$_POST['id'].")};</script>";
}
Related
I am using file_get_contents (http://example.com/books.php?id=55).
I am using search and replace function but not working.
<?php
$homepage = file_get_contents('http://example.com/category.php?c=55');
echo $homepage;
// Search replace not working
// str_replace("http://localhost/", "http://example.com/", $homepage );
?>
Out-Put**
<a class="touch" href="/textbook/book1.php">Book1</a>
<a class="touch" href="/textbook/book2.php"> Book2</a>
<a class="touch" href="/textbook/book3.php">Book3</a>
If i click on url it show http://localhost/textbook/book1.php. But I want http://mysite[dot]com/link.php?id=http://example.com/textbook/book1.php
Try this
<?php
$homepage = file_get_contents('http://example.com/category.php?c=55');
$homepage = str_replace("textbook/", "http://localhost/xx/link.php?id=http://example.com/textbook/", $homepage );
echo $homepage;
?>
There is no "localhost" in the code you parsed. So replacing "localhost" will do nothing. You just need to find the link using href and add your custom link. Also there might be any text "textbook" in your HTML.
Try this:
<?php
$homepage = file_get_contents('http://example.com/category.php?c=55');
$str=str_replace("href=\"", "href=\"http://newsite.com/link.php?id=http://example.com/", $homepage );
echo $str;
I would like to store JavaScript code in a PHP variable, in order to inject it multiple times into my main code:
<?php
$PASSME = <<<PASSME
alert("hello world");
PASSME;
echo "<a onclick=$PASSME >Click here</a>";
?>
In Google Chrome I can read this source code:
<a onclick="alert("hello" world");>Click here</a>
So I noticed this:
"hello" world" should be "hello world"
What am I doing wrong?
NB: I am actually working on a more complex project. I tried to make an example in order to understand how to correctly do it.
As I commented you used double quoetes in double quotes, use single quotes instead:
<?php
$PASSME = <<<PASSME
alert('hello world');
PASSME;
echo "<a onclick=\"$PASSME\" >Click here</a>";
?>
This will result in correct code:
<a onclick="alert('hello world');">Click here</a>
When having a lot of code, just pass variables from php to js, ie:
<?php
$PASSME = <<<PASSME
var message = 'hello world'
PASSME;
?>
<script>
<?= $PASSME; ?>
</script>
<?
echo "<a onclick=\"alert(message)\">Click here</a>";
?>
Use following (You missed Quotes around variable)
<?php
$PASSME = <<<PASSME
alert("hello world");
PASSME;
echo "<a onclick='".$PASSME."' >Click here</a>";
?>
The problem is that your attribute value contains space characters and is not delimited with quote characters.
<?php
$html_safe_passme = htmlspecialchars($PASSME, ENT_QUOTES);
?>
<a onclick="<?php echo $html_safe_passme; ?>">Click here</a>
You need to escape the " to " in the HTML attribute value. You also need to delimit the attribute value with double-quotes (which mustn't be encoded) because it contains spaces, like so:
(Also, personally I wouldn't use PHP's <<< for strings)
$passme = "alert("hello world");";
echo "<a onclick=\"$passme\">click here</a>";
Try it like this, with single quotes:
alert('hello world');
<?php
$PASSME = "alert('hello world');";
echo "<a onclick=". $PASSME . " >Click here</a>";
?>
try to add to change space in your code. and DONE!
I have here my href link being echoed:
echo "<td><a href='../php/borrowersname.php?acc_number=".$row['acc_number']."'>".$row['title']."</a></td>";
And I have here a sample on how to create a Pop-Up:
Test
How to combine this to my href link above being echoed with the "javascript ... ..." enclosed by ("" && '')?
Thanks for another new learning.
You can escape the ""
<?php
echo "Test";
?>
In the 'borrowersname.php' page, you can get the value of 'acc_number' like this:
<?php
$value = $_GET['acc_number'];
//do something with $value;
?>
I had an onclick event as below.
<div onclick="display_function('<?php echo $user_id;?>','<?php echo $student_id;?>','<?php echo $student_name;?>')"></div>
function display_function(user_id,student_id,student_name)
{
alert(user_id+'-'+student_id+'-'+student_name); //<-- testing only. I have my own code here
}
the function works fine with the name like Mary, Chris and etc.
However, if the student name contains a ', e.g. Cheng'li, the function won't work.
I need help to fix this. How can I make the function works by 'escaping' the quote mark in name?
Thanks.
You need to add a call to htmlentities around the data you wish to echo.
Not doing so exposes your code to XSS attacks.
use PHP function addslashes
<?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>
IN your case
<?php echo addslashes($student_name);?>
REFERENCE
http://www.php.net/addslashes
Note: If your code contain html tag than use htmlentities (Entoarox Answer)
you can either use escape()
<div onclick="display_function(escape('<?php echo $user_id;?>'),escape('<?php echo $student_id;?>'),escape('<?php echo $student_name;?>'))"></div>
function display_function(user_id,student_id,student_name)
{
alert(user_id+'-'+student_id+'-'+student_name); //<-- testing only. I have my own code here
}
That is because you are passing the values in function in single quotes. When name will have a single quote, this will cause error.
try double quotes like
<div onclick="display_function(\"<?php echo $user_id;?>\",\"<?php echo $student_id;?>\",\"<?php echo $student_name;?>\")"></div>
Just add \ before ' to tell your script that it is a string. I hope it helps
<?php
$user_id = 1;
$student_id = 1;
$student_name = "Cheng\'li";
?>
<div onclick="display_function('<?php echo $user_id;?>','<?php echo $student_id;?>','<?php echo $student_name;?>')">Click</div>
<script>
function display_function(user_id,student_id,student_name)
{
alert(user_id+'-'+student_id+'-'+student_name); //<-- testing only. I have my own code here
}
</script>
If you cannot put \ directly in String, you need to use [addslashes][1]
<script>
function display_function(user_id,student_id,student_name)
{
alert(user_id+'-'+student_id+'-'+addslashes(student_name)); //<-- testing only. I have my own code here
}
</script>
I have the above below already functional on my website. It works alright but right now, I want to create a kind of promo and if the display meets the criteria of the promo, I want it to display onclick $htx
<?php
echo "<a href='javascript:;' onclick='pkgsPopup('http://'.$hLnk');' rel='nofollow'>";
?>
I have $htx pre defined to open a link $dealpath and if it does not meet that condition, I want it to open the default link - '$hLnk'
I have tried the code below and I had an error _ I mean the page will not load at all
if ($htx) { echo "onclick=\'miaPopup('http://$dealPth');\'' } else { echo 'onclick=\'pkgsPopup('http://$hLnk');\'' }";
I will really appreciate if someone can let me know how to do this without error using the PHP if/else statement.
Here is your second block of code a little cleaner. You were missing semi-colons in the PHP as well as matching escape characters ( a mix of apostrophes and quotes)
if ($htx)
{
echo "onclick=\'miaPopup('http://$dealPth');\'" ;
}
else
{
echo "onclick=\'pkgsPopup('http://$hLnk');\'" ;
}
The problem is that you have the echo pumping out a long string that makes no sense:
if ($htx) { echo "onclick=\'miaPopup('http://$dealPth');\'' } else { echo 'onclick=\'pkgsPopup('http://$hLnk');\'' }";
should read:
if ($htx) {
echo "onclick='miaPopup(\'http://$dealPth\');'";
} else {
echo "onclick='pkgsPopup(\'http://$hLnk\');'";
}
You could simplify by saying
if ($htx) {
$url = $dealPth;
} else {
$url = $hLnk;
}
echo "onclick='pkgsPopup(\'http://$url\');'";
Something like this should work (this code is not tested):
$onclick = ($htx ? 'miaPopup("http://'.$dealPth.'");'
: 'pkgsPopup("http://'.$hLnk.'");');
echo "<a href='javascript:;' onclick='$onclick' rel='nofollow'>";
try this
if ($htx){
$onclick="miaPopup('http://$dealPth');";
}
else{
$onclick="pkgsPopup('http://$hLnk');";
}";
echo "<a href='javascript:;' onclick='$onclick' rel='nofollow'>";
Why not use jQuery
<a class='one <?=($cond?"two" : "")?>'>Link</link>
<script>
$(function(){
$(".one").click(function(){
//Class of one
});
$(".two").click(function(){
//class of two
})
})
</script>