简体   繁体   中英

Calling PHP from document.ready

Is there a way in document ready to call php scripts?

I want to do something like this.

<script>
    $(document).ready(function($) {

    <?php require("readenglish.php"); ?>
    <?php require("readfrench.php"); ?>
    <?php require("readspanish.php"); ?>
    <?php 

    $opload = $_GET['opload'];
    if ($opload == "reade") {

    }
    else if ($opload == "readf") {

       echo "<script type=\"text/javascript\">\n";

       echo "document.f1.r1[0].checked = false;\n";
       echo "document.f1.r1[1].checked = true;\n";
       echo "SelectRead();\n";

       echo "</script>";

    }

    ?>

    });

</script>

The three php scripts create divs and add information to them from an external domain php script.

You're mixing client and server side with no interface there. You're going to have to use $.ajax to pull in the content from the php scripts and update your DOM accordingly.

Of course... you could also design your site in a way that makes sense. This method looks suspicious at best and almost positively has a much better, exclusively server side solution.

You could do something like this in your page:

$(document).ready(function($) {
   $('#result').load('test.php', function() {
    alert('Load was performed.');
  });
});

Where, on the server, test.php contains your code:

<?php require("readenglish.php"); ?>
<?php require("readfrench.php"); ?>
<?php require("readspanish.php"); ?>
<?php 

$opload = $_GET['opload'];
if ($opload == "reade") {

}
else if ($opload == "readf") {

   echo "<script type=\"text/javascript\">\n";

   echo "document.f1.r1[0].checked = false;\n";
   echo "document.f1.r1[1].checked = true;\n";
   echo "SelectRead();\n";

   echo "</script>";

}

This then needs to be added back into the DOM on the client side. Not good design though.

I would recommend using jQuery. They have a load method that does exactly what you're asking: http://api.jquery.com/load/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM