John Davidson

How to embed php in javascript?

0 comments
Message:


Face problem when writing PHP syntax in javascript syntax.


var id = $("#data-1").val();
var url = '<?= base_url('home/alone/'); ?>.'id'';
console.log(url);

I need to place the id at the end of url. But my code is not working. What is wrong?
Thank you in advance.



My Answer:
The issue in your code is with the way you are concatenating the id variable with the url string. You should use the + operator to concatenate the variables in JavaScript. Here is the corrected code:

javascript
var id = $("#data-1").val();
var url = '' + id;
console.log(url);


By using the + operator, you can concatenate the id variable with the url string to form the complete URL. This should solve the issue you are facing.

Rate this post

5 of 5 based on 5965 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us