Welcome Guest, Not a member yet? Register   Sign In
Getting value from a javascript variable into a php variable???
#1

[eluser]Roy MJ[/eluser]
Hi,

Can anyone help me in telling me how to get a javascript variable to transfer its value to a variable in php.??? I used a javascript for a multiple select box and the multiple selection is working fine. But i dunno how to get the value from the javascript variable which stores the result.

My controller looks like this :

Code:
function add(){
            $data['tourcategory'] = $this->Tour_packages_model->select_category();
            $data['destination'] = $this->Tour_packages_model->select_destination();
            //if data submitted ' '
            $this->load->plugin('js_calendar');
            if($this->input->post('Submit')){
            $config = array(
                     array('field'   => 'destination', 'label'   => 'Destination', 'rules'   => '')      
                    );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('admin/tour_packages/add',$data);
            }
            else{
                    $this->Tour_packages_model->save('Add', $data);
                    redirect('/admin/tour_packages');
                }
        }else{
                $this->load->view('admin/tour_packages/add',$data);
        }    
    }


My model page looks like this :

Code:
function save($type='',$field='')
    {
            switch($type){
                case 'Add':
                $data = array(
                'destination' => $this->input->post('destination')
                                            
                );
                                        
            $this->db->insert('tour_package', $data);
            break;
     }

And my view looks like this :

Code:
<head>

</head>

<table border=0>
            <tr>
            <td valign="top" width=175>
            <label for="departure">Destinations:* </label>
            <br>
            <select name="available"  style="width:150px" size=10>
            &lt;?php
            foreach($destination as $row)
            {
            ?&gt;
            <option value=&lt;?php echo $row->destination?&gt;>&lt;?php echo $row->destination?&gt;
            &lt;?php
            }
            ?&gt;
            </select>
            </td>
            <td valign="top">
            Your Choices:
            <br>
            <select multiple name="destination" style="width:150px;" size="10">
            </select>
            </td>
            </tr>
            <tr>
            <td colspan=2 height=10>
            &lt;input type="button" value="Remove"&gt;
            &lt;input type="button" value="Get Selected Values"&gt;
            </td>
            </td>
            </tr>
            </table>


         <button value="Send" name="Submit" id="submit-go" type="submit">Add</button>

I am not able to post the javascript linked with it. This forum is showing one error.

The save part of the script is shown below:






[/code]




The final result is stored in the variable strValues i think. But i cannot find a way as to get the result into a variable outside the script so that the i can add the data obtained into the database. Please help..
#2

[eluser]Roy MJ[/eluser]
var strValues = "";
var boxLength = document.choiceForm.destination.length;
var count = 0;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.destination.options[i].value;
}
else {
strValues = strValues + "," + document.choiceForm.destination.options[i].value;
[removed](strValues);
}
count++;
}
}
alert("Here are the values you've selected:\r\n" + strValues);
#3

[eluser]Atharva[/eluser]
If you want to store the value into database, you will need to use Ajax. Use jQuery. jQuery Ajax
#4

[eluser]Roy MJ[/eluser]
Can u please tell me something that i can do with the script.. Im not familiar with ajax or javascript. I have used javascripts for like lightbox and stuff.
#5

[eluser]Atharva[/eluser]
Another way is to store the JavaScript variables in hidden fields, and submit the form in javascript, so that they can be accessed via $_GET or $_POST in a php script.
#6

[eluser]Roy MJ[/eluser]
Will this work?

&lt;input type='hidden' name='des' value='strValues'&gt;
#7

[eluser]Atharva[/eluser]
[quote author="Roy MJ" date="1294745210"]Will this work?

&lt;input type='hidden' id='des' name='des' value='strValues'&gt;[/quote]

Nope
Code:
[removed]
document.getElementById('des').value = strValues;
[removed]
Note additional attribute id='des'
#8

[eluser]Roy MJ[/eluser]
Im sorry for disturbing so much, but im still only learning my way with these scripts.

I have tried including the above script to the javascript and tried calling using $_GET['des'];

But now its showing error:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: des

Filename: admin/tour_packages.php

Line Number: 27
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/edatour/public_html/soumya/system/libraries/Exceptions.php:164)

Filename: helpers/url_helper.php

Line Number: 588
#9

[eluser]Atharva[/eluser]
I don't see &lt;form&gt; in your view so can't tell which method you are using. If it is post, then you can access it by
Code:
$this->input->post('des')
If it is get then you can have your function defined as
Code:
function xyz($des){}

so that you can access 'des' as a uri segment.

P.S : Though I mentioned $_GET in my earlier post, it is not allowed in CI and it gets filtered out.
#10

[eluser]Roy MJ[/eluser]
Thanks a lot for your time. But im still stuck with this. The olny value thats getting added into my database is 0 and nothing else.. Im looking to do something else for the time..




Theme © iAndrew 2016 - Forum software by © MyBB