0

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 Application',
                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!

1
  • 2
    If you want my advise, you should learn the difference between server-side languages and client side ones, BEFORE even hearing about jQuery ;). Commented Jan 25, 2011 at 10:10

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

Hi. Thank you for your response!! I did what you said and I receive a "Successful" message. But when I put the $employee->save() after the "when successfully build", I dont get any message and the records dont get saved either. Do you have any idea why is that? Thanks!
Did you put it before or after ` echo json_encode(array('status'=>true, 'message'=>'Successfull')); exit;` or echo 'Successfull'; exit; ?? because when you do Exit; to stop everything whats after exit; php.net/manual/en/function.exit.php
I tried it both ways and it doesnt work. I actually fixed some things and now im getting a "failed" and not a successful. I tried for the Modal to open when I click on the submit button, but I also have the validate action I guess in that button, so when I do an onclick="openModal()", neither the validate nor the modal work.
And also, if I do it like that, then how will I be able to put the values in the modal "first name: John, last name: Smith" etc... in order to confirm it? Sorry for the many questions!! Thanks for everything!
I did it! y just had to add type:'Post' ! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.