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)."');\">";
?>
Related
I want to concatenate the value of my variable $variable with the value of my input using javascirpt
$variable= file_get_contents('./env.txt');
<select id="customer_id" name="customer_id" w300 required pj-chosen" data-msg-required="<?php __('lblFieldRequired');?>" onchange="document.getElementById('title').value=this.value + 'P' + **$variable**">
...
<input type="text" name="title" id="title" class="pj-form-field w400 required" data-msg-required="<?php __('lblFieldRequired');?>" data-msg-remote="<?php __('lblTitleUsed');?>" value=""><script></script>
Ok so why not echo out the variable?
PHP:
onchange="document.getElementById('title').value=this.value + 'P' + '<?php echo addslashes($variable) ?>'">
The addslashes call will escape quotes against security problems.
If $variable does not change a more elegant way is to make the $variable a JavaScript variable and then use it normally. This makes the JavaScript part more readable, but of course other programmers will have to be aware that you add that variable via PHP.
PHP:
// somewhere at the beginning of your body tag:
<script>
window.variable = '<?php echo addslashes($variable) ?>';
</script>
HTML:
<... onchange="document.getElementById('title').value=this.value + 'P' + window.variable" />
I wrote this off the top of my head - please tell if this worked as I intended.
On the line below you've already got some PHP inline in the HTML, outputting values into those data attributes.
For $variable you can follow much the same pattern as that, except you'll need to explicitly echo the variable so it gets embedded into the JavaScript (I assume those other functions echo within them, removing the need for another one):
onchange="document.getElementById('title').value=this.value + 'P' + '<?php echo addslashes($variable); ?>'"
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"/>
I Tried lot but can't solve this problem
Here is my Code:
<?php
while($crow = mysqli_fetch_assoc($cres)) {
echo '
<div class="item" onclick="window.open("'.$crow["c_link"].'");window.open("'.$crow["c_link"].'","_self")<img src="images/carousel/'.$crow["c_pic"].'" alt="'.$crow['c_nm'].'"></div>
';
}
?>
There is error Like this:
I also tried another answer of this problem but it is not working for me.
can anybody please tell me how to solve this ?
Problem is with mixed quotes and aprostrophes
onclick="window.open("
You open onclick attribute value with " character and then close the attribute just after window.open(. Instead of the second " use \' or IMO even better close PHP mode, print HTML code and open PHP mode only to print PHP values
while($crow = mysqli_fetch_assoc($cres)) {
?><div class="item" onclick="window.open('<?=$crow['c_link']?>'); window.open('<?=$crow['c_link']?>');"><img ... ></div><?php
}
?>
this way it's easier to not get confused by combination of JS and PHP quotes and apostrophes.
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.