as the title already says I am calling a onclick method in php, but every string with a space in it seems to fail.
php (mysql loop)
echo '<img onclick=test("'.$row['name'].'","name'.$row['id'].'") src="blabla.png"/>';
javascript
function test(vali, id){
alert(vali); //Maria, Josef (but not "Michael Jackson")
alert(id); //name23, name28
}
As I said if its a string without a space, alert method is getting called and shows the text, however with at least one space nothing happens.
Your whole onclick handler needs to be quoted. You then need to also HTML-escape the quotes inside it.
echo '<img onclick="test("'.$row['name'].'","name'.$row['id'].'")" src="blabla.png"/>';
In the HTML this will then appear as
<img onclick="test("A Name With Spaces","name1")" src="blabla.png"/>
And the javascript part is ultimately decoded to
test("A Name With Spaces","name1")
You can use it like below
<img onclick='test("<?php echo $row['name']; ?>",name="<?php echo $row['id']; ?>")' src="blabla.png"/>
Related
I'm new to javascript.
There seems to be no result but a blank screen when I try the following code:
function send_to_class($data)
{
echo 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP';
?>
<script type="text/javascript">
$(".username").append(<?php echo $data;?>);
</script>
<?php
echo 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE';
}
and my PHP
$data = "You might think I'm Bonkers, but I just think I'm Free";
send_to_class($data);
and my html
<span class="username"></span>
The Function is declared before the send_to_class($data) is initiated.
The P's and the E's are showing up just fine, there's just no middle sentence.
I've tried divs, id's and classes, adding css and changing the script tag.
2 things, a) how do I get the function to work which appends $data to the .username class and b) how do I get it to work if $data is an array? Can Javascript print php arrays in this manner?
Thanks.
I found that because the $data I was sending contained "'s that it needed to have the "'s escaped. So in my php file that was producing the data, I had to turn the following:
<div class="class1">some words</div>
into this:
<div class=\"class1\">some words</div>
This is because it's javascript that's doing the printing, and when javascript does printing of "'s it needs to have them escaped with a backslash like this \".
I am trying to update innerHTML property of paragraph tag in PHP. I made a rest API call that gets the date and after parsing it. I am adding the result(HTML table constructed by parsing the results from API) to a <P> tag already in page. I am using following PHP code to do so:
$dom = new DOMDocument();
$child = $dom->createElement('p',$myresult);
$dom->appendChild($child);
$dom->SaveHTML();
$myresult is the string that contain html table with information. The above lines are executed smoothly but no change in p tag content.
I tried this too, but no change in output:
<?php echo "<script>document.getElementByID("#id").innerHTML = ". $myresult."</script>"
Am I doing anything wrong?
try this
<?php echo "<script>document.getElementByID(\"id\").innerHTML = ". $myresult."</script>" ?>
you need escaping quotation on getElementByID and u dont need #
You should escape double quotes inside a double-quote string in PHP. But, you can as well use single quotes instead like so:
<?php echo "<script> document.getElementById('id').innerHTML = '$myresult' </script>"; ?>
Also, it is getElementById not getElementByID. And remove the # from getElementById().
Try:
<?php echo "<script>document.getElementById(\"id\").innerHTML=".$myresult."</script>" ?>
as long as id is an existing html element id and $myresult is a php variable containing html
OR
You may try
<div id="id"><?php echo $myresult; ?></div>
Notes:
1. It is getElementById() not getElementByID()
2. Usually #id is used in jquery, and in pure JS it is just id
I have the following script, which returns 2 values from my database.
$(document).ready(function() {
<?
$link=new mysqli($serveur,$login,$pass,$base);
mysqli_set_charset($link, "utf8");
$r=mysqli_query($link, "select sujet,corps from mail where type='Création_rationnaire'");
while($row = mysqli_fetch_array($r)) {
?>
document.getElementById("sujet").value="<? echo $row[sujet];?>";
document.getElementById("te").value="<? echo $row[corps];?>";
<? }mysqli_close($link); ?>
});
Here are my 2 forms, one is a classic Input, the other is a textarea.
<input id='sujet' class="form-field">
<textarea id="te" class="textarea" rows="9" cols="70"></textarea>
The problem is : my SQL request works in MySQL, but when "corps" is echoed as above, nothing appears in the Inputs, like the script was crashing... Only "sujet" can be echoed successfully. So... why, just why ? Thanks in advance for whoever helps me.
EDIT : all I can echo in the "te" textbox is a single string like this : echo 'something';... but nothing else works...
Try this
document.getElementById("sujet").value="<? echo $row['sujet'];?>";
document.getElementById("te").text="<? echo $row['corps'];?>";
or if you use jquery
var message = <?php echo $row['corps']; ?>;
$("#te").val(message);
Textarea does not have a value but it does have text.
in an input box you would have <input type="text" value="example" /> and in a text area the value is inside the element like so
<textarea>example</textarea>
A other way is to use innerHtml (javascript) or html (jquery) .
But note that html tags will be display with one of these options
document.getElementById("te").innerHTML="<? echo $row['corps'];?>";
<textarea> doesn't have value property. Since you are using jQuery to bind document ready, you can use jQuery to set the value.
$("#sujet").val("<? echo $row[sujet];?>");
$("#te").html("<? echo $row[corps];?>");
What is the value of $row[corps]? Is it empty string? Does it contain double quote, or newline characters? If it contains any of them, it will break your javascript source code. What does javascript source code look like when you check source code inside the browser?
It works by inserting the values directly into the plain html forms. I usually use this javascript function because I have tons of forms to fill in my program, and it never failed until now... So my function above was good, but we still don't know why it failed in this particular case...
Problem solved anyways... thanks to everyone.
I have onchange event inside a select input, I try execute a javascript function but I need insert inside one value get from php as follows:
<?php
print "<select name='sel_reg' id='testsel2' onchange=\"loaddiver('city',this.value ".$_REQUEST['data_loc'].");\">";
?>
The problem come from $_REQUEST['data_loc'] if i put inside no works nothing if i don´t use this , all works perfectly , i need get this 2 values inside but i don´t know how i can do this
You don't have a comma after this.value:
onchange="loaddriver('city', this.value, $_REQUEST['data_loc']);
Try This.
<?php
?>
<select name="sel_reg" id="testsel2" onchange="loaddiver('city',this.value, <?php echo $_REQUEST['data_loc'] ;?>)">
<?php
?>
First thing: Your data is uneascaped, so if some html special char ("'<> etc.) occurrs in the string it'll mess up your html.
Second thing: You need apostrophes around your string and you need to add a comma after this.value:
<?php
print "<select name='sel_reg' id='testsel2' onchange=\"loaddiver('city', this.value, '".htmlentities($_REQUEST['data_loc'], ENT_QUOTES)."');\">";
?>
I have this piece of code, which doesn't work, I couldn't find where is the problem?
$texttt = "blabla";
echo "<div onclick='select(\" page d \' accueil \");'>".$texttt."</div><br/>";
function select(text){
alert(text);
}
" is breaking your php statement
echo "<div onclick=\"select(' page d\' accueil ');\">\"Page d'accueil\"</div><br/>";
function select(text){
alert(text);
}
try to use addslashes and htmlentities to escape the quotes between text
$texttt = htmlentities(addslashes($text), ENT_QUOTES);
echo "<div onclick='select(\"$texttt\");'>$texttt</div><br/>";
Use single quotes for PHP. I use single quotes for PHP (which is server side script) and double quotes for client side scripts unless it is not obligatory.
echo '<div onclick="select(\'page d\'accueil\');">"Page d\'accueil"</div><br/>';
the problem is sourced by single quote in d'accueil:
I dont know if it is possible to escape a string which is located in another escaped string.
The possibilities:
You can change the grammar rules of your language:
Affects too many people.
Not applicable.
You can change the way of typing single quote: using ' in context.
I have typed a new code (which is working well) for you :
<?php
echo '<div onclick="select(this.innerHTML);">"Page d'accueil"</div><br/>';
?>
<script>
function select(text){
alert(text);
}
</script>
you should avoid useless quotes before you get lost inside.
"Page d'accueil" don't need to be between quotes so remove them.
Use one kind (single or double quotes) for the first level of quoting, and the other for the next levels, and keep that it mind.
<? php echo '<div onclick="select(\" page d\' accueil \");">Page d\'accueil</div><br/>'; ?>
<script type="text/javascript">
function select(text){
alert(text);
}
</script>
you can use heredoc syntax
refreence : http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
also try this:
echo json_encode("<div onclick='select(" page d ' accueil ");'>{$texttt}</div><br/>");