1

ページをリロードせずにロードするためのボタンクリックのindex.php呼び出しがあります。「url.php」には、ajax を介して呼び出す別のボタンがあります。url.phpajaxdata.php

今何が起こるか: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ページをロードせずにロードする必要があります。

index.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>

url.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>

data.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.phpおよびurl.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 に感謝

肝心なのは、ボタンクリックで呼び出されるすべての関数を、アクションを開始した最初のファイルに配置する必要があることです。つまりindex.php、後続のファイルには配置しません。url.php

于 2013-09-25T06:18:49.587 に答える