1

I have index.php which call url.php on button click with ajax to load without page reload. Now on 'url.php' there's another button which call data.php through ajax.

Right now what happens: When open index.php first and click button on index.php it load url.php without page load. but when I click button Fire me on url.php it redirects to data.php

If directly I open url.php then it performs ajax action and loads data.php without page reload.

What should I make change here so that : When I open first index.php and click button on it, it load url.php without page reload(this already happens, prblem with next step). After that when I click button on 'url.php' it should load data.php without page load.

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



}

?>

Bottom line is: creating ajax/jquery system index.php -> url.php -> data.php. Existing index.php -> url.php and url.php -> data.php

6
  • so what is the question or problem? Commented Sep 25, 2013 at 5:34
  • @ihsan: What should I make change here so that : When I open first index.php and click button on it, it load url.php without page reload(this already happens, prblem with next step). After that when I click button on 'url.php' it should load data.php without page load. Commented Sep 25, 2013 at 5:41
  • Please avoid copying large chunks of code into your files without understanding what they actually do. Half the code you posted here is listed twice and doing nothing. Commented Sep 25, 2013 at 5:50
  • @mavrosxristoforos: yes you right. I am not very sound with with ajax, jquery. I really could not decide which code do nothing. So I am carrying it with me! Commented Sep 25, 2013 at 5:59
  • I will be greateful if you can help me to remove it! Commented Sep 25, 2013 at 5:59

2 Answers 2

1

In url.php you have

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

That will definitely cause the form to post. Instead write

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

And add

return false;

at the end of the showUser function.

EDIT: After various changes in question contents:

I noticed that you are loading the whole url.php into the body of the index.php. Yet, you have <html>, <head> and <script> tags in the url.php which I doubt if they are called. That's why the form just submits itself.

The correct way to go is: Write ALL your Javascript in the index.php file, and just load the new page contents by loading the url.php. I was about to write an example, but I saw that you updated your question code, so I hope this cleared things out.

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

6 Comments

thanks, but problem till exist. While I click button on url.php it load and redirect to data.php
Problem is with Fire me button. Please check. It loads data.php on click which is the action I have kept in form.
Why don't you update your question code, according to the changes I suggested, also removing the huge parts of unused JS form url.php ? That would help other people understand what you are asking and possibly help another person with the same problem some day in the future.
but till it does not do what I want!
Ok got it. I need to place all function code in my first file index.php
|
1

thanks to @mavrosxristoforos

The bottom line is I need to place all the function those being called on buttonclick into first file from which I started the action i.e. index.php not in subsequent files url.php

Comments

Your Answer

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