CodeIgniter Forums
Passing javascript variable to PHP variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Passing javascript variable to PHP variable (/showthread.php?tid=19725)



Passing javascript variable to PHP variable - El Forum - 06-16-2009

[eluser]1mr3yn[/eluser]
Good day,,
Im using the jquery modal form.,. I have a problem with this JavaScript and php hope someone help me..Im trying to pass a variable from javascript to PHP, I tried a lot but it did'nt work.,.
Here's my sample code:

$("#dialog").dialog({
bgiframe: true,
autoOpen: false,
height: 300,
modal: true,
buttons: {
'Add New Partner': function() {
var bValid = true;
allFields.removeClass('ui-state-error');

bValid = bValid && checkLength(cat,"Category",1,4);
bValid = bValid && checkLength(code,"Partner Code",1,5);
bValid = bValid && checkLength(desc,"Description",5,20);

bValid = bValid && checkRegexp(code,/^([0-9a-zA-Z])+$/,"Product Code only allow : a-z 0-9 ");
bValid = bValid && checkRegexp(desc,/^([0-9a-zA-Z])+$/,"Description field only allow : a-z 0-9");

if (bValid) {
// Here's the main problem
// insert the value from cat.val(), code.val() and desc.val() into the database table

$(this).dialog('close');
}
},
Cancel: function() {
$(this).dialog('close');
}
},
close: function() {
allFields.val('').removeClass('ui-state-error');
}
});


Passing javascript variable to PHP variable - El Forum - 06-16-2009

[eluser]1mr3yn[/eluser]
and im currently using codeigniter for this whole stuff...


Passing javascript variable to PHP variable - El Forum - 06-17-2009

[eluser]ok3x[/eluser]
As I see it, your jQ code runs on the presentation side (the browser). And the CI resides on the server side. So you have to pass the data along. While using jQ, I suggest you make an ajax request:
Code:
if (bValid) {  
   // ajax request
   $.post("http://yoursite.com/xhr/handle",
      { cat: cat.val(), code: code.val(), desc: desc.val() }, // data to send JSON-hash encoded
      success: function(msg){
         $('#status').html(msg); // success callback - simply echo the message with php
         $(this).dialog(‘close’);
      },
      error: finction(){
          $('#status').html('Something went wrong');  // error callback
      }
   );
}
For more options check the jQ documentation.

And at the server side:
Quote:function handle()
{
var_dump($_POST); // just to understand what's arrived
$cat = $this->input->post('cat');
// and so on
// save data to database
echo 'data saved'; // ajax response
}



Passing javascript variable to PHP variable - El Forum - 10-30-2010

[eluser]Unknown[/eluser]
Can be an article from this site will help you : Passing Javascript variable to php