To use the rayan.gigs and antyrat answers, the following is a fix for the rayan.gigs answer and how you get this information from the controller:
fixed answer rayan.gigs (modified to match the controller example below):
// your html <script> var myOrderString = "something"; $.ajax({ type: "POST", url: <?= site_url('OrderController/receiveJavascriptVar'); ?> data: {"myOrderString": myOrderString}, // fix: need to append your data to the call success: function (data) { } }); </script>
jQuery AJAX docs explain the various functions and options available for AJAX functions, and I recommend reading it if you are not familiar with jQuery AJAX Functionality. Even if you do not need a successful callback, you can still implement the error to retry the AJAX call, register the error, or notify the user if necessary.
antyrat (modified to match the controller example below):
<form action="<?= site_url('OrderController/receiveJavascriptVar'); ?>" method="POST" id="myForm"> <input type="hidden" name="myOrderString" id="myOrderString"> </form> <script> var myOrderString = "something"; document.getElementById('myOrderString').value = myOrderString; document.getElementById('myForm'); </script>
The controller may look like this for both of the above cases:
<?php class OrderController extends CI_Controller { public function receiveJavascriptVar() { $myJSVar = $this->input->post('myOrderString');
I would recommend the rayan.gigs method instead of overusing forms. However, if you are trying to send user information or send information along with an existing form submission, you can use the antyrat method and simply insert the hidden one that you fill out either for rendering or through javascript.
source share