如何在jquery函数中运行php函数?

时间:2011-01-25 10:00:06

标签: php jquery function

我想在jquery模式框中单击确认按钮后运行PHP函数。这是代码:

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

    $confirm = true;
}

&GT;

    <?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 Application',
                maxWidth: 500,
                buttons: {
                    'Confirm': function(win) { openModal(); },
                    'Cancel': function(win) { win.closeModal(); }
                }
            });
        }

    // Demo modal
        openModal();

});
</script>

<?php } ?>

然后,如果该人按下“确认”,我想运行“$ employee-&gt; save()”功能。我怎么能做到这一点?!谢谢!

2 个答案:

答案 0 :(得分:1)

直接你无法运行你可以使用ajax调用

编辑:在ajax页面中调用它,同时使用进程栏阻止页面

答案 1 :(得分:0)

您应该将ajax请求发送到使用PHP类$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>

我应该在我的ajax帖子中使用我的php文件:

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;
};