1

我有index.php哪个调用url.php按钮单击ajax以加载而无需重新加载页面。现在在'url.php' 上有另一个data.php通过ajax 调用的按钮。

现在会发生什么:index.php首先打开并单击它的按钮index.php 时加载url.php而不加载页面。但是当我单击按钮Fire me时,url.php它会重定向到data.php

如果我直接打开url.php,那么它会执行 ajax 操作并加载 data.php而无需重新加载页面。

我应该在这里进行什么更改,以便:当我首先打开index.php并单击它的按钮时,它会在url.php不重新加载页面的情况下加载(这已经发生,下一步会出现问题)。之后,当我单击“url.php”上的按钮时,它应该data.php在没有页面加载的情况下加载。

索引.php:

    <?php
    session_start();
    $appId      = '178812862';
    $appSecret  = '390c044f268aca44'; // Facebook App Secret
    $return_url = 'dev01.dev/FB/facebook-php-sdk/examples/'; //path to script folder
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
     <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

    </script>
        <title>ThenWat</title>

        function AjaxResponse()
     {
        //alert("2");
        var mydata;         
        CallAfterLogin(mydata, function(send) {
                   });
      } 
    function LodingAnimate() //Show loading Image
    {
        //alert("animate4");    
      //  $("#LoginButton").hide(); //hide login button once user authorize the application
    }

        </script>

   <script type="text/javascript">
                //------------------script 2 starts ---------
                function showUser(form, e) {
                e.preventDefault();
                e.returnValue=false;
                var xmlhttp;
                var sent = form.elements['sent'].value;

                if (window.XMLHttpRequest) {
                    xmlhttp=new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function(e) {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open(form.method, form.action, true);
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
                xmlhttp.send('sent=' + sent);

                return false;
                }

             </script>


    </head>
    <body style="height: 560px">
    <?php
    if (!isset($_SESSION['logged_in'])) {
    ?>

       <div style="z-index: 1; left: 538px; top: 290px; position: absolute;"> 
        <input class="uibutton confirm large" type="submit" value="Connect with facebook" onclick="AjaxResponse()" > 
        </div>
        <?php
    }
    ?>


    <div id="fb-root"></div>
    <script type="text/javascript">
    window.fbAsyncInit = function() {
    FB.init({
        appId: '<?php
    echo $appId;
    ?>',
        cookie: true,
        xfbml: true,
    //  channelUrl: '<?php
    echo $return_url;
    ?>channel.php',
        oauth: true});};
    (function() {
            //alert("1");
        var e = document.createElement('script');
        e.async = true;
        e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);}());
    function CallAfterLogin(data,callback){
            //alert("CallafterLogin3");
            FB.login(function(response) {  //---
            if (response.status === "connected")
            {
                LodingAnimate(); 
                FB.api('/me?fields=movies,email', function(mydata) {
                console.log(mydata.email);

                //alert("json-5");
                var json = JSON.stringify(mydata.movies.data);
            //    alert(mydata.movies);
                var a = JSON.parse(json);       
                $.post('movies_db.php',{'myd':a}, function(data) 
                {
                    $.ajax({
                url:'url.php'
                ,async:     true
                ,cache:     false
                ,dataType:  'html'
                ,success:   function(data){
                    $('body').html(data);
                        }
            });        
                });
            console.log(data);

                  }); //--
             } //if
                 }); //---
              } //main function
    </script>
    </body>
    </html>

网址.php

<html>
   <head>
      <title>ThenWat</title>

    <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
    <meta content="utf-8" http-equiv="encoding" />
        </head>
   <body style="height: 560px">
        <form action="data.php" method="POST" onsubmit="return showUser(this, event)">
            <div style="z-index: 1; left: 470px; top: 100px; position: absolute; margin-top: 0px">              
               <label>Enter URL: <input type="text" value="http://www.paulgraham.com/herd.html" name="sent" style="width: 400px; height:40px;" ></label><br/>
            </div>
            <div style="z-index: 1; left: 420px; top: 160px; position: absolute; margin-top: 0px">  <button> Fire me </button>      
            </div>
         </form>
         <div style="z-index: 1; left: 660px; top: 160px; position: absolute; margin-top: 0px"> 
            <button onclick="makeAjaxCall(); return false;" value="View Graph" > View Graph </button>
         </div>
    <h3>
         <div id="txtHint" style="z-index: 1; color:green; left: 200px; top: 270px; position: absolute; margin-top: 0px">       </h3>
         </div>

   </body>
</html>

数据.php

<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile($url);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
foreach($xpath->query("//script") as $script) {
    $script->parentNode->removeChild($script);
}
$textContent = $doc->textContent; //inherited from DOMNode
$text=escapeshellarg($textContent);
$test = preg_replace("/[^a-zA-Z]+/", " ", html_entity_decode($text));


//--
    $flag = 0;
    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Desktop/b '$test'"),  $matches)){ //Values stored in ma. 
    $x = (int) $matches[1][0]; //optionally cast to int
    $y = (int) $matches[1][1];
    $p= (100*$x)/($x+$y);
    $q= (100*$y)/($x+$y);
    echo "<b>Positive count : $x with $p % </b>";  echo "</br>";
    echo "<b>Negative count  : $y with $q % </b>";

    $con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
    if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$flag=0;
// $sql = "INSERT INTO interest (movies,id) VALUES('".$Name."','".$ID."')";
$sql1 = "INSERT INTO table2 (id,sent,pcount,ncount,flag) VALUES ('".$x."','".$url."','".$x."','".$y."','".$flag."')";
if (mysqli_query($con, $sql1)) {
  // echo "1 record added";
} else {
   die('Error: ' . mysqli_error($con));
}


mysqli_close($con);



}

?>

底线是:创建 ajax/jquery 系统 index.php -> url.php -> data.php。现有index.php -> url.phpurl.php -> data.php

4

2 回答 2

1

在 url.php 你有

<form action="data.php" method="POST" onsubmit="showUser(this, event)">

这肯定会导致表单发布。而是写

<form action="data.php" method="POST" onsubmit="return showUser(this, event);">

并添加

return false;

showUser函数的最后。

编辑:在问题内容发生各种变化后:

我注意到您正在将整个 url.php 加载到 index.php 的正文中。然而,你在 url.php 中有<html>,<head><script>标签,我怀疑它们是否被调用。这就是表单只是提交自己的原因。

正确的做法是:将所有 Javascript 写入 index.php 文件,然后通过加载 url.php 来加载新页面内容。我正要写一个例子,但我看到你更新了你的问题代码,所以我希望这能解决问题。

于 2013-09-25T05:34:40.497 回答
1

感谢@mavrosxristoforos

底线是我需要将所有在 buttonclick 上调用的函数放入我开始操作的第一个文件中,即index.php不在后续文件中url.php

于 2013-09-25T06:18:49.587 回答