using php string with line breaks as javascript variable - javascript

I was working on project in which I have to get some text from the database and display it in the text area. Working fine with no line breaks and tabs. but when a line breaks occurs it gives the error
Uncaught SyntaxError: Invalid or unexpected token
My java script code is as :
<script type="application/javascript">
$(document).ready(function () {
document.getElementById('job_type').value = "<?php echo set_value('job_type') ?>";
document.getElementById('job_cat').value = "<?php echo set_value('job_cat') ?>";
if("<?php if(validation_errors() == false) echo "false"; else echo "true"; ?>"=="false")
{
$('#job_title').val("<?php echo $job[0]->job_title ?>");
$('#job_type').val("<?php echo $job[0]->job_type ?>");
$('#job_cat').val("<?php echo $job[0]->job_cat ?>");
$('#salary').val("<?php echo $job[0]->salery ?>");
$('#job_descp').text("<?php echo $job[0]->job_desc ?>");//error here
}
});
});
</script>
The text it was trying to set was
Error details are shown in images
Debug info
I have tried using .html() and .val() too but nothing worked. How can I handle it.

This has already been answered here https://stackoverflow.com/a/4270709/3004335
However, you can use json_encode
<script type="application/javascript">
$(document).ready(function () {
document.getElementById('job_type').value = "<?php echo set_value('job_type') ?>";
document.getElementById('job_cat').value = "<?php echo set_value('job_cat') ?>";
if("<?php if(validation_errors() == false) echo "false"; else echo "true"; ?>"=="false")
{
$('#job_title').val(<?php echo json_encode($job[0]->job_title); ?>);
$('#job_type').val(<?php echo json_encode($job[0]->job_type); ?>);
$('#job_cat').val(<?php echo json_encode($job[0]->job_cat); ?>);
$('#salary').val(<?php echo json_encode($job[0]->salery); ?>);
$('#job_descp').text(<?php echo json_encode($job[0]->job_desc); ?>);//error here
}
});
});
</script>
You do however need to be careful about cross site scripting
See here https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet in particular rule #0

Remove line break(CR LF) in job[0]->job_desc
like this:
$('#job_descp').text("<?php echo str_replace (array("\r\n", "\n", "\r"), '<br>', $job[0]->job_desc);?>");

Related

Echoing a mulitple line message in alert function

I need to echo multiple line error in JS alert and I am using below code
Working code
$str = "This is a error1\\n";
$alert = $str."This is error2";
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
but I need to work on an old written code so this has addslashes function before echo alert like below:
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
Output : This is a error1\nThis is error2
I need this to print in two lines I have also tried with only \n but it is not working then.
I'd make javascript do the hard work.
<script type="text/javascript">
var alertArr = ["<?php echo $string1; ?>", "<?php echo $string2; ?>"];
alert(alertArr.join("\n"));
</script>
https://jsfiddle.net/0uwmmm3n/
(Magnificent random url by the way)
EDIT: less messy version, maybe more legitimate
<?php $alertArr = [$string1, $string2]; ?>
<script type="text/javascript">
var alertArr = <?php echo json_encode($alertArr); ?>;
alert(alertArr.join("\n"));
</script>
RE-EDIT: nah, too much work for a job so simple, two conversions is way too much
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
I have used this code and it working fine according to your need
if still issue continues then clear your cache

How to alert PHP value in JavaScript?

<script type="text/javascript">
var jvalue = 'this is javascript value';
<?php
$abc = "<script>document.write(jvalue)</script>" ?>
</script>
<?php echo 'php_'.$abc; ?>
<script type="text/javascript">
var test = "<?php echo $abc; ?>";
</script>
<?php
echo '<script language="javascript">';
echo 'alert(test)';
echo '</script>';
?>
How to alert test variable, which contains PHP value?
I would like to get the PHP value in javascript, for further execution in my project.
try this code
<?php
echo '<script language="javascript">alert("'.$test.'"); </script>';
?>
this is updated code for you using javascript and PHP
<?php
$user = "rebel";
echo '<script> var name = "'.$user.'";
alert(name);</script>';
?>
This is the third code for you if this does not work then you have other problem
<?php
$abc= "Hello";
?>
<script type="text/javascript">
var test="<?php echo $abc; ?>";
</script>
<?php
echo '<script language="javascript">';
echo 'alert(test)';
echo '</script>';
?>
why are you assigning the variable to a javascript variable and then echoing that? sounds like extra work to me...
echo 'alert("' . $abc . '");';
Your code is not so clear, i think you want something like this:
<?php
$test = "hi there!";
echo '<script type="text/javascript">';
echo 'alert("'.$test.'")';
echo '</script>';
?>
var test="<?php echo $abc; ?>"
<script language="javascript">alert(test); </script>
If you want to alert the value of $abc, you need to escape the slash in the following line with a backslash, like this.
<?php $abc = "<script>document.write(jvalue)<\/script>" ?>
You also need to remove this line (or escape the < and the > in $abc). If you don't, the script tags will mess up the rendered html code.
<?php echo 'php_'.$abc; ?>

how to convert the jquery element text() into string

I can not successfully get the string using the jquery text()
I have my HTML
<span>/</span>
<span onclick="
var inner_text=$.trim($(this).text());
if(inner_text=='Send Request'){
var post_id=<?php echo $blog_id ;?>;
var event_date='<?php echo $blog_feeds['valid_date'] ?>';
var post_text='<?php echo addslashes($blog_feeds['post_text']) ?>'; //if the string contains ' , then would have error
var request_text_id='<?php echo $blog_id.'comment_text';?>';
var request_content=$('#'+request_text_id).val();
var who_post='<?php echo $blog_feeds['username'] ;?>';
$.ajax({
url:'php/join_request.php',
method:'post',
data:{request_content:request_content,post_id:post_id,who_post:who_post,event_date:event_date,post_text:post_text},
success:function(data){
if(data=='ok'){
$('<?php echo '#'.$blog_id.'request_w' ?>').html('You have sent '+who_post+' a joining reuqest on this event');
$('<?php echo '#'.$blog_id.'request_button'; ?>').html('Request Sent');
$('<?php echo '#'.$blog_id.'request_button'; ?>').css('opacity','0.7');
$('<?php echo '#'.$blog_id.'request_button'; ?>').css('cursor','initial');
$('<?php echo '#'.$blog_id.'comment_text';?>').val('');
}
}
})
}
" id="<?php echo $blog_id.'request_button'; ?>" class="j_request_button">
<?php
$select_request=mysql_query("SELECT `id` FROM `join_request` WHERE `post_id`='$blog_id' AND `who_request`='$username'");
if(mysql_num_rows($select_request)>=1){
echo '<span style="opacity:0.7;cursor:initial">Request Sent</span>';
}
else{
echo '<span style="cursor:pointer">Send Request</span>';
}
?>
</span>
if I add a alert(inner_text) right before if statement, I can successfully alert "sent request",but I can not alert ok within the if statement
Try:
var inner_text=$.trim($(this).text());
This will remove any surrounding whitespace, which is likely if you have line breaks between your spans.
Don't put a full script like that in your HTML. Give your span an id, and use jQuery in a separate script tag:
<span id="sent_request">sent request</span>
<script>
$('#sent_request').click(function(){
var inner_text=$(this).text();
if(inner_text=='sent request'){
alert('ok'); //not working
}
});
</script>

Run PHP echoed Javascript

I am just getting into the world of PHP, Javascript and HTML and would like some help from the community regarding the following code. Basically I want to pass variables plucked from a ODBC_connection by PHP into textboxes. Lines 1-3 were for me to test to get the box to update which it does but anything echoed by PHP does not run. I am completely new to this so I realize I must be missing something trivial.
I welcome any suggestions or comments about what I can do to fix this or what I can do better in general.
Thank you.
<script type='text/javascript'>
document.getElementById('modeltxt').value = "test2";
</script>
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('modeltxt').value =\"TEST3\";";
echo "document.getElementById('customertxt').value = $customer;";
echo "document.getElementById('endusertxt').value = $enduser;";
echo "document.getElementById(dongletxt').value = $dongle;";
echo "document.getElementById('shipdatetxt').value = $shipdate;";
echo "document.getElementById('chasistypetxt').value = $chasistype;";
echo "document.getElementById('chasisnumbertxt').value = $chasisnumber;";
echo "document.getElementById('opsystxt').value = $opsys;";
echo "document.getElementById('dvd1txt').value = $dvd1;";
echo "document.getElementById('dvd2txt').value = $dvd2;";
echo "document.getElementById('storagetxt').value = $storage;";
echo "document.getElementById('nodrivetxt').value = $nodrive;";
echo "document.getElementById('drivesizetxt').value = $drivesize;";
echo "document.getElementById('interface1txt').value = $interface1;";
echo "document.getElementById('interface2txt').value = $interface2;";
echo "document.getElementById('interface3txt').value = $interface3;";
echo "document.getElementById('interface4txt').value = $interface4;";
echo "document.getElementById('interface5txt').value = $interface5;";
echo "document.getElementById('interface6txt').value = $interface6;";
echo "document.getElementById('commentstxt').value = $comments;";
echo "document.getElementById('warrantyexptxt').value = $warrantyexp;";
echo "document.getElementById('extendedwarrantytxt').value = $extwarexp;";
echo "document.getElementById('onsitetxt').value = $onsite;";
echo "document.getElementById('sqlversiontxt').value = $sqlversion;";
echo "<\script>";
You can create dynamics JS using the below
Define Content-Type on the top of your .js.php file:
<?
header('Content-Type: application/javascript');
// Write your php code
?>
and call the js file like this ..
<script type="application/javascript" src="JS_PATH/name-of-file.js.php"></script>
and if you want to use inline php values, you can write like this
<script type="application/javascript">
document.getElementById('modeltxt').value = "<?php echo $dummy_value ?>";
</script>
No need to do this. You can simply use javascript inside php as below :-
As you mention in your question
Basically I want to pass variables plucked from a ODBC_connection by PHP into textboxes.
<?php
// Php block of code
?>
<script type="text/javscript">
document.getElementById('modeltxt').value = "TEST3";
document.getElementById('customertxt').value = "<?php echo $customer;?>";
......
....
...
</script>
<?php
// Another block of code
?>

Why this Javascrript Error Is coming at line 2 character 1

I have The Following Javascript Error
"Line 2 Character 1" coming in the entire app
I have researched Follwing thing in SOF i have found this link .
Javascript Line 1 Syntax error in IE
So my JS script is something like this
<script type ="text/javascript">
$(document).ready(function(){
var sv = "<?php echo $var1 ;>";
var sv1 = "<?php echo $var2 ;>";
var sv2 = "<?php echo $var3 ;>";
var sv3 = "<?php echo $var4 ;>";
var sv4 = "<?php echo $var5 ;>";
});
</script>
But when i put the same code under JSLINT it is saying "Problem at line 1 character 2: Expected 'html' and instead saw 'script'."
.How can I make by script corrected .Can any one please explain what iam doing wrong
You need to declare html tags first, and THEN you can start typing your script.
Wrap it in tags.
You're also having some weird PHP tags there, they need to be finished with ?>, not ;>
You aren't properly closing your PHP tags
<script type ="text/javascript">
$(document).ready(function(){
var sv = "<?php echo $var1 ?>";
var sv1 = "<?php echo $var2 ?>";
var sv2 = "<?php echo $var3 ?>";
var sv3 = "<?php echo $var4 ?>";
var sv4 = "<?php echo $var5 ?>";
});
</script>
Are you sending the PHP directly to the browser? If not, we'll need to see the code as it appears to the browser. If there was a double quote being printed, for example, it would cause syntax errors.
Also, there shouldn't be whitespace between your type attribute and the = character.

Categories