Sending javascript variable from view to controller in Codeignitor

Possible duplicate:
How to get javascript function data in php variable

I have a javascript variable in the order.php view and I want to send this variable to the controller and then to the model to write it to the database.

ex: var myOrderString = "something" for the order.php controller.

how to do it? I searched google for this for weeks still havent found out. please help! Thanks in advance!

+4
source share
3 answers

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'); // push to model } } ?> 

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.

+2
source

You want to send var from the view to the anther controller ..... this means a new request

This is easy to do with an Ajax request.

In view

  // your html <script> var X = "bla bla "; $.ajax ({ type: "POST", url: // ur controller, success: function (html) { } }); </script> 
0
source

You can create a simple html <form> with a hidden field. Assign a JS variable to this field and submit the form.

A simple example:

 <form action="order.php" id="myForm"> <input type="hidden" name="myOrderString" id="myOrderString"> </form> <script> var myOrderString = "something"; document.getElementById('myOrderString').value = myOrderString; document.getElementById('myForm'); </script> 

Now in order.php you will have the _GET value of myOrderString .

You can also do this simply with AJAX.

0
source

Source: https://habr.com/ru/post/1411713/


All Articles