tags:

views:

318

answers:

3

Hello.

Could some please advise how to include a php file to update content in a div upon successful Ajax form submit?

This works:

jQuery(form).ajaxSubmit({
  success: function() {
$('#ajaxRenew').load('script.php');  
}
});

but my script.php requires headers, etc, from the page it gets included into (it works ok as "Include" normally), and with .load it does not get them, producing warnings.

I read that Ajax is to be used in this case (.replace?), but I am trying the whole day and can not get it work.

I need something like this:

jQuery(form).ajaxSubmit({
  success: function() {
$('#ajaxRenew').replace('script.php');  
}
});

I hope you will understand my question. Thank you!

+1  A: 

Not possible, I don't think. As soon as the php context ends (ie, when the script is done executing the first time), you lose the includes. You should be able to do all the relevant includes inside your script.php, and pass any parameters that need to carry through from POST or GET via ajaxSubmit.

Josh
Thank you very much!
Josh is correct. If, for example you need your database login info on your script.php page, you will have to include that on that page.
superUntitled
A: 

Pass the variables needed to the Ajax/load function. This will create an Ajax POST request with the data specified in the 2nd argument.

For example,

<?php
//index.php
$variable = time();
?>
<div id="myDiv">
</div>
<script type="text/javascript">
    $('#myDiv').load('responder.php', {"hello" : <?= $variable ?>});  
</script>
?>



<?php
//responder.php
print_r($_POST);
?>
Robert Duncan
Thank you very much!
A: 

You can let your script return the div's content. Maybe it's not the best practice but you can do something like this with JQuery.

$('#formId').submit(function ()
{
    $.post('fle.php', {variable1: 'value'}, function (data)
    {
        /* Check data contents, maybe it's returned as json or what ever */
        $('#divId').html(data);
    }, 'json'); /* Skip the last option if you're not using json */
});

If you have to include a whole file I think .load is the only option.

Kristinn Örn Sigurðsson