Welcome Guest, Not a member yet? Register   Sign In
Passing javascript variable to PHP variable
#1

[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');
}
});
#2

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

[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
}
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB