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.
Related
A lot of queries raised targeting this issue. i.e. line breaks within a textarea attribute.
I tried to use str_replace("<br />", "\n",$text) within a javascript variable where I got it working within a php code. Unfortunatley had no such luck with the methodology I am using within the javascript code.
The code I am trying to use is as follows:
var markup = "<textarea name='tcaction[]' id='tcaction' rows='3' cols='105' placeholder='Enter Required Actions' required><?php echo str_replace("<br />", "\n",$text) ?></textarea><br>";
The str_replace within the javascript variable is not working. Would you cordially direct me to the right direction?
Thanks for assistance.
Replace this ...
...<?php echo str_replace("<br />", "\n",$text) ?>...
^^
... by this:
...<?php echo str_replace("<br />", "\\n",$text) ?>...
^^^
So PHP sends \n (2 chars) and JavaScript interprets it as newline character.
All, for the interest of others the issue has been resolved as follows:
var text = <?php echo json_encode($text); ?>
and then used
text.replace("<br />", "\n");
it works and no issues of un-escaped new line etc etc.
thanks anyway.
Hi I have a problem while using set interval function with .load events and append please explain what I did mistake in my code please give a solution
HTML
<div id="user2<?php echo $d['qid'] ;?>">
Comments Loads Here
Jquery code
setInterval(function(){
$("#user2").load( "test2.php,#user2");
},1000);
qid means Query id I have multiple(qid) queries in my site How to add append
please give a solution I am new in jquery Thanks in advance
Id of div is dynamic, as "qid" is appended to it.
<div id="user2<?php echo $d['qid'] ;?>">
In above code, if "$d['qid']" is 101, then generated html will be as given below.
<div id="user2101">
Hence, your javascript must include dynamic id using "" syntax. Also, you should be using space instead of comma in load event (http://api.jquery.com/load/#loading-page-fragments). You should be passing "qid" to "test2.php" page as query string to get comments related to "qid".
setInterval(function(){
$("#user2<?php echo $d['qid'] ;?>").load( "test2.php #user2");
},1000);
First, as qid is the query id. Having the following code with qid 2 will yield
<div id="user22">
Is this really the div you really want? If you want user2 it should be:
<div id="user<?php echo $d['qid'] ;?>">
Second, you need to pass a valid path to your page you want to load. Are you sure test2.php,#user2 is the correct path?
I try to create a suggest text input field read the suggest information from mysql database when a user enter a letter for example A all word contain letter A appear under the input field i use PHP and mysql.
The HTML
<label for="email">Location *</label>
<input type="text" list="mylocation" id="suggest" name="txt_location">
<datalist id="mylocation">
</datalist>
</div>
PHP code
<?php
include("../includes/connect.php");
$location=$_GET['txt_location'];
$sql=mysqli_query($conn,"select DISTINCT(db_location) from tbl_marketing where db_location like '$location%' order by db_location")or die(mysqli_error($conn));
if(mysqli_num_rows($sql)>0){
while($row=mysqli_fetch_array($conn)){
$loc=$row['db_location'];
echo"<option value='$loc'>";
}}
?>
My Script
$(document).ready(function(){
$("#suggest").keyup(function(){
$.get("suggest.php",{location: $("#suggest").val()},function(data){
$("datalist").empty();
$("datalist").html(data);
});
});
});
I use also in my website bootstrap
The problem is that my code didn't work. Is there any other method can i do that or can i use bootstrap to do it and What is and How to do it ??!!
Try the following code. Hope it works.
$(document).ready(function(){
$(document).on('keyup',"#suggest",function(){
$.get("suggest.php",{txt_location: $("#suggest").val()},function(data){
console.log(data);
$("#mylocation").empty();
$("#mylocation").append(data);
});
});
});
Troubleshooting steps:
Check if datalist is working. (Add some dummy <option> set)
Check if suggest.php is connected and providing you result. Use console.log.
Basically your script is correct except the txt_location and location i.e., key value pair mismatch.
try this PHP code
<?php
include("../includes/connect.php");
$location=$_GET['location'];
$sql=mysqli_query($conn,"select DISTINCT(db_location) from tbl_marketing where db_location like '".$location."%' order by db_location")or die(mysqli_error($conn));
if(mysqli_num_rows($sql)>0){
while($row=mysqli_fetch_array($conn)){
$loc=$row['db_location'];
echo"<option value='$loc'>";
}
}
?>
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 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)."');\">";
?>