1

just a simple question, can we assigned codeigniter open_form() to javascript var?

i have a code like this:

var openForm = '<?php echo form_open("controller/some_function", 
array('class' => 'class_name', 'enctype' => 'multipart/form-data'));?>';

but when i run it, i got error in my console saying:

Uncaught SyntaxError: Invalid or unexpected token

but, when i try this:

var closeForm = '<?php echo form_close(); ?>';

it didn't show any error.

though i guess it's not about syntax error, i still have no idea what is wrong and what happens. can anyone explain?

2
  • What you are trying to achieve here? Do you want dynamically append HTML or codeigniter form somewhere? Commented Jul 17, 2016 at 9:30
  • @ShrikantMavlankar yeah, dynamically append html. Commented Jul 17, 2016 at 9:59

1 Answer 1

1

Yes. You can use like this

var openForm = `<?php echo form_open("controller/some_function", array("class" => "class_name", "enctype" => "multipart/form-data")); ?>`;

openForm += '<?php echo form_close(); ?>';

$("#your_element)id").html(openForm);

In javascript You can't split a string across multiple lines. <?php echo form_open(); ?> add \n at the end which create Syntax error.

As well <?php echo form_open(); ?> adds double quotes, which also ends into escaping issues.

To avoid issues In such cases you can use the template literals which is `

For more details you can visit this site. I hope it justifies your query.

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

2 Comments

You're welcome. Can you please up vote and accept my answer? :)
Yes. ` is called back-tick where as ' is called as single quote. Template literals are enclosed by the back-tick where as strings can be enclosed with single quote or double quote.

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.