4

i am assigning PHP var to my javascript var and sending to PHP file through ajax-jQuery, but my php variable contains newline chars which i have replaced with <br>

e.g.            $values1 = 'abc<br>pqr<br>xyz';  $values2 = 'xyz<br>lmn';
javascript -    var data = 'val1=<?php echo $values; ?>&val2=<?php echo $values2; ?>';

and then ajax script to post data to PHP file

but when i print this data on console it is giving me error- SyntaxError: unterminated string literal.

Can anyone help ?

3

3 Answers 3

2

Your JS code:

var data = 'val1=<?php echo $values1; ?>&val2=<?php echo $values2; ?>';

Will give Javascript syntax error if one or more of your PHP variables $values1 OR $values2 contain single quote ' in them.

Make sure your PHP variable don't contain single quotes in them by replacing all single quotes to something else otherwise use double quotes " to create JS var like this:

var data = "val1=<?php echo $values1; ?>&val2=<?php echo $values2; ?>";

Provided PHP variables don't contain double quotes.

Sign up to request clarification or add additional context in comments.

15 Comments

i dont have issue with ajax data post to php, but even though when i try to print on console e.g. console.log('<?php echo $values1; ?>') it is giving error
You need to print it like this: console.log("<?php echo $values1; ?>") actually.
sorry, my mistake here,i am already using console.log('<?php echo $values1; ?>'), still same error
Can you print it like this: console.log("<?php echo $values1; ?>"); note I am using " intead of '
And you're viewing this output in shell or browser? If inside shell then this is indeed the cause of the problem since your string contains new line characters.
|
0

First of all, you have a typo, that might cause an error:

// --------------------------------v
var data = 'val1=<?php echo $values1; ?>&val2=<?php echo $values2; ?>';

Then, I suggest you to use object as data parameter for Ajax request:

var data = {
    val1: '<?php echo $values1; ?>',
    val2: '<?php echo $values2; ?>'
};

Also it is better to escape single quotes ' in both $values1 and $values2 variables.

6 Comments

thanks Vision , but i dont think its typo, coz it is working if i remove <br>.
i dont have issue with ajax data post to php, but even though when i try to print on console e.g. console.log('<?php echo $values1; ?>') it is giving error
You should surround the value with quotes: console.log('<?php echo $values1; ?>').
sorry, my mistake here,i am already using console.log('<?php echo $values1; ?>'), still same error
Try to add slashes: console.log('<?php echo addslashes($values1); ?>');.
|
-1

Try using <br /> instead of <br>. Just guessing here, no testing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.