Not see data on the textarea - javascript

not see data on the textarea
creatDocument.php
<html>
<head>
<script type="text/javascript" src="../jquery-1.8.1.js"></script>
<script language="javascript">
function insertFormulationToTextErea(string){
document.getElementById("argumentId").value = string;
}
</script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<textarea name="argument" id="argumentId" rows="10" cols="50"></textarea>
<?php
if(isset($_REQUEST['formulation']))
$formulation = $_REQUEST['formulation'];
else $formulation = "No data receive";
$formulation = base64_decode($formulation);
echo "<script>insertFormulationToTextErea('$formulation')
</script>";
echo $formulation;
?>
</body>
</html>
$formulation is the string parameter that received from another php page with POST method.
When I use with echo I see the string value, when I try to insert $formulation to textarea, the textarea is empty.
When I use with GET method it work fine, but when $formulation string too long, the server reported that Request-URI Too Large.
Anybody know this problem or any solution that I can use?

What you're doing looks a bit complex, try simply this:
<textarea name="argument" id="argumentId" rows="10" cols="50" value="<?php echo $formulation; ?>"></textarea>
This puts your string directly into the textarea value, without trying to use it as a parameter for your function.
AND reason why your you get nothing from $formulation when you use it as a paramter for your function is because your code should be like so:
echo "<script>insertFormulationToTextErea(".$formulation.")</script>";
Though I highly advise you to avoid coding like that..

Related

Ajax live search no output

Hello I am a beginner in php and jQuery so an early sorry if my question is stupid. I have been searching quite a lot on ajax live search but there is not much help on internet... So when I type something in my input box nothing comes out, so I thought it was a problem to connect to the database but that works fine. And now I don't really know what to do because my code sounds right (even though it's not :-) ). If someone could help me it would be great thank you.
Here are the codes:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>live search test</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script scr="jquery.js"></script>
<script type="text/javascript">
functon getNames(value) {
$.post("fetch.php",{partialName:value},function(data)
$("#results").html(data);
});
}
</script>
</head>
<body>
<h1>LIVE SEARCH WITH AJAX TEST</h1>
<input type="text" onkeyup="getNames(this.value)">
<br>
<div id="results">
</div>
</body>
</html>
PHP:
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("smartphone")or die(mysql_error());
$partialName = $_POST['partialName'];
$names = mysql_query("SELECT name FROM smartphone WHERE name LIKE '%$partialName%'");
while ($name = mysql_fetch_array($names)) {
echo"<div>".$name['name']."</div>";
}
?>
I think if you check your browser console tool, you will see '$ is undefined'.
Unless it was for this example, the following is incorrect:
<script scr="jquery.js"></script>
It should be:
<script src="jquery.js"></script>
Please also switch your insecure SQL query to the secure PDO bound parameters style, to avoid being exploited.

Error while using valiable in Jquery.val() funciton

I got no output when I use variable in val() function instead of fixed value. How can I fix this error.
Example: In the below example, when I use variable ($data="Glenn Quagmire"), I get no output.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val($data);
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
Regards
Just use like this:
$("input:text").val("<?php echo $data; ?>");
Full code
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?php echo $data; ?>");
});
});
</script>
Don't use "$" for variables (that's PHP) in jQuery.
<?php echo 'data="Glenn Quagmire";'; ?>
$("input:text").val(data);
JavaScript running in the browser cannot access variables from a PHP program (which has finished running on another computer before delivering the page to the browser so the variable does't even exist any more).
You have to make PHP output the data to the JavaScript.
You can encode it using json_encode (because JSON is (more or less) a subset of JavaScript literal syntax). This will add quotes as needed and escape any special characters in your data (you don't have any in your example, but this is a safe, general approach that protects you from XSS).
$("input:text").val(<?php echo json_encode($data); ?>);
Use <?= $variable ?> to embed variable values in HTML, e.g.:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?= $data ?>");
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>

Sending a JSON encoded PHP variable to JS

I am trying to send a PHP variable to my javascript. In order to do this, I encode the php variable with json, send it to my view with my controller, use it as a value in a hidden html form, and then pull the value with js, using parseJSON to get the final product.
HTML (I'm using laravel, the brackets are equivalent to php tags):
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value="{{$artist_likes}}">
JS
var artist_likes = $('#js-helper-artist-likes').val();
console.log(artist_likes);
In the page source, I see that the value is populating with the json string, but when I print it to the console from my js like the above, all that is showing is: [{
I am not sure why this is.
I want to perform:
var artist_likes_decoded = $.parseJSON(artist_likes);
to get the decoded variable in JS, but this is impossible without the encoded string coming up correctly in the artist_likes variable in js.
Any ideas what I'm doing wrong? Thank you.
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value="{{ echo $artist_likes}}">
You could use:
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value="<?= json_encode($artist_likes) ?>">
<?php
$yourVariable ="something";
$arr = json_encode(array("name"=>$yourVariable));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<input type="hidden" id="js-helper-artist-likes" name="js-helper-artist-likes" value='<?php echo $arr; ?>'>
<script>
$(document).ready(function(){
var artist_likes = $('#js-helper-artist-likes').val();
console.log($.parseJSON(artist_likes));
});
</script>
</body>
</html>

PHP Code Error fopen not opening and writing

When I run this PHP code it should store the comment but it doesn't write anything in comments.txt as it was supposed to do. Please find the error.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<?php
setcookie("username","admin", time()+3600);
setcookie("password","xss", time()+3600);
if($_POST['content']!=null){
$fp= fopen('comments.txt','a');
fwrite($fp,$_POST['content'], "</hr>");
fclose($fp);
}
echo nl2br(file_get_contents('comments.txt'));
?>
<h3>Post Your HTML Code here</h3>
<form action="index.php" method="post">
<textarea name="content" rows="3" cols="100"></textarea>
<br/>
<input type="submit" value="Post" />
</form>
</body>
</html>
You can see a demo of this on my website here.
have you checked your file has right permissions for apache to write it?
you can change permissions with chmod, an example:
// allow any operation for any user
chmod("comments.txt", 0777);
if($_POST['content']!=null){
The preferred (or preached) way to handle what you're after with this line is:
if(isset($_POST['content'])) {
For the actual error, it's because you're using the wrong arguments for fwrite(). From the PHP documentation, the arguments are:
int fwrite ( resource $handle , string $string [, int $length ] )
Drop the "</hr>" from the end and it'll fix it, like so:
fwrite($fp,$_POST['content']);

using a php variable in a JS funcion

i previously asked this question but the example i provided did not provide my real problem.
I want to get the input from a form using a php variable and use it in a javascript function. I do not use javascript directly because the content of the variable in question is obtained from a mysql database and i do not think that there is a way for me to access the database using javascript.
below its the code that i have so far:
<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var slon1 = '<?php echo (json_encode($siteLon1)); ?>';
self.alert(slon1);
//some code goes here.
}
</script>
</head>
<body>
<div align="center">
<form method="get">
<label>Choose Site</label>
<select name='ps' id="ps" data-inline="true">
<?php
$use = $fgmembersite->getsites($fgmembersite->UserFullName());
$fields_num = mysql_num_fields($use);
while($row = mysql_fetch_row($use))
{
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
?>
</select>
<input type="submit" name="submit" value="View">
</form>
</div>
<div id="map-canvas"></div>
</body>
</html>
Suprisingly enough, the result displayed on self.alert(slon1) is null; be it when i just load the page or when choose from the drop down box and click on View. Please someone help me on this.
Thanks.
JSON encoder should quote variable, so using single quotes manually would trigger JavaScript parse error. Check JS console. This should generate valid JS code:
var slon1 = <?php echo (json_encode($siteLon1)); ?>;

Categories

Resources