开发者

How can I run a php function in a jquery function?

I want to run a PHP function once I click a confirm button in a jquery modal box. Here is the code:

<?php
if(isset($_POST['submitBackground'])){
    $employee = new Employee();
    $employee->build($_POST);

    $confirm = true;
}

?>

    <?php if($confirm){ ?>
<script type="text/javascript">


$(document).ready(function(){

    // Demo modal
        function openModal()
        {
            $.modal({
                content: '<p>Please make sure that all the information is correct</p>'+

                          '<ul class="simple-list with-icon">'+
                          '    <li>First Name:</li>'+
                          '    <li>Last Name:</li>'+
                          '    <li>Address:</li>'+
                                                  '    <li>City:</li>'+
                                                  '    <li>State:</li>'+
                                                  '    <li>Zip Code:</li>'+
                                                  '    <li>Position:</li>'+
                                                  '    <li>Social Security #:</li>'+
                                                  '    <li>Drivers License:</li>'+
                                                  '    <li>Drivers License State:</li>'+
                          '</ul>',
                title: 'Confirm Applicati开发者_开发百科on',
                maxWidth: 500,
                buttons: {
                    'Confirm': function(win) { openModal(); },
                    'Cancel': function(win) { win.closeModal(); }
                }
            });
        }

    // Demo modal
        openModal();

});
</script>

<?php } ?>

Then, if the person presses "Confirm" I want to run the "$employee->save()" function. How can I get that done?! Thank you!


directly you cannot run you can call using ajax

EDIT : Call it in ajax page and at the same time you block the page using process bar


You should send a ajax request to a file where you use your PHP class $employee->save()

<script type="text/javascript">

function ButtonSaveClick()
{
 $.ajax({
   url: '[.. to your php file ..]',
   data: $('#employeeFrm').serialize(), //using serialize to POST every field (<input />)
   dataType: 'json', //only if you wanna return with JSON
   success: function(data) {
  // callback when done posting
  //when doing json
  if(data.status)
  {
   alert(data.message);
   // refreshList() or somthing
  }
  else
  {
   alert(data.message);
  };
   }
 });
}

$(document).ready(function(){

    // Demo modal
 function openModal()
 {
  $.modal({
   content: '<p>Please make sure that all the information is correct</p>'+
   '<form id="employeeFrm">'+  
   '<ul class="simple-list with-icon">'+
   '    <li>First Name:</li>'+
   '    <li>Last Name:</li>'+
   '    <li>Address:</li>'+
   '    <li>City:</li>'+
   '    <li>State:</li>'+
   '    <li>Zip Code:</li>'+
   '    <li>Position:</li>'+
   '    <li>Social Security #:</li>'+
   '    <li>Drivers License:</li>'+
   '    <li>Drivers License State:</li>'+
   '</ul>'+
   '</form>',
   title: 'Confirm Application',
   maxWidth: 500,
   buttons: {
    'Confirm': function(win) { ButtonSaveClick(); },
    'Cancel': function(win) { win.closeModal(); }
   }
  });
 }

    // Demo modal
    openModal();

});
</script>

So should my php file i use in my ajax post:

include_once "[dbfile]";
include_once "[employeeClass is stored]";

$employee = new Employee();
if($employee->build($_POST))
{
 //when successfully build
 //return json way
 echo json_encode(array('status'=>true, 'message'=>'Successfull')); exit;
 //return non json way
 echo 'Successfull'; exit;
}
else
{
 //when failed with building
 //return json way
 echo json_encode(array('status'=>false, 'message'=>'failed')); exit;
 //return non json way
 echo 'failed'; exit;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜