Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By cyberion1985

Thread: PHP reloads page

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Member #
    39665
    Liked
    1 times

    PHP reloads page

    Hello Everyone ,

    I am struggling with PHP. It's working, just not how I want it to work. I must add, I am a complete noob in php, html and so on coding. Here are some of my codes :

    index.php html form :
    Code:
        <form action="index.php" method="POST">
            <input type="text" name="query" class="abcdef"/><br/>
    	    <input type="submit" value="Search"/>
        </form>
    index.php php code :
    Code:
    <?php
    
    $host = "data";
    $user = "data";
    $pass = "data";
    $dbase = "data";
    $dtabl = "data";
    
        $con = mysqli_connect($host, $user, $pass, $dbase) or die("Error connecting to database: ".mysqli_error());
    echo "<div id='ovisible'>";
         
    echo "<div><a href='#' onclick='ovisible()'><b>close</b></a><br/><h1><u> Search Results</u></h1>";	
    
    	if (isset($_POST['query']))
    	{
    	$query = $_POST['query'];
    
        if(strlen($query))
    	{
            $query = htmlspecialchars($query);
             
            $query = mysqli_real_escape_string($con,$query);
             
            $raw_results = mysqli_query($con,"SELECT * FROM data WHERE (`cname` LIKE '%".$query."%') ") or die(mysqli_error());
              
            if(mysqli_num_rows($raw_results) > 0)
    		{ 
                 
                while($results = mysqli_fetch_array($raw_results)){
    
                 
                    echo "<p><h3>".$results['name']."</h3>".$results['surname']."</p>";
                }
            }
            else
    		{ 
                echo "No results";
            }
    	}
    	}
    	else{echo "No search defined ! ";}
    echo "</div>";
      
    echo "</div>";
    css
    Code:
    #ovisible{display:none;position: absolute;left: 0px;top: 0px;width:100%;z-index: 1000;background-color:#000;filter:alpha(opacity=80);text-align:center;-moz-opacity:0.9;opacity:0.9
    html head function
    Code:
    <script>
    
    function osearch()
    {
    	xyz = document.getElementById("ovisible");
    	xyz.style.display = (xyz.style.display == "block") ? "none" : "block";
    }
    </script>
    Lange Rede kurzer sinn : I am trying to show the results INSIDE my overlay (ovisible) WITHOUT the page reloading on form (action="index.php"). If I add onclick="ovisible()" to the input type=submit in the form, then it will open and close the overlay because action="index.php" will reload the page. If I set action="index.php" to action="", action="#" then it doesn't work. Removing the entire input button and placing it beneath the form works, but doesn't return the text to the php and thus it doesn't compute the text box string at all :

    Code:
        <form action="index.php" method="POST">
            <input type="text" name="query" class="abcdef"/><br/>
    
        </form>
    
    	    <input type="submit" value="Search" onclick="ovisible()"/>
    I would like to know, if there is a easy way to get the results to display on my index.php page, without reloading my page ? Someone on another forum, mentioned I should
    You can create your search.php to return only JSON string, and making an AJAX call using javascript fetch the JSON response from the search.php and render the data using JavaScript.

    You can use JQuery or AngularJS. Both will do the needful for you.
    and I was like ... huh ? I have no idea where to start or what that even means. Is that even English ? See, I would like the above to work, as it does actually work in the sense that it just isn't displayed where and how I want it : The functionality works nicely. Here are a few examples of how I would like to see it done : None that some code and commands are mere imaginations :

    1.) the input submit must show the overlay page with results

    2.) Maybe I can remove the php code completely and add it to the "search.php" file and use action="search.php" BUT somehow forge the results INTO a page WITHIN a page almost like inception

    3.) I rewrite the entire php into java or similar with my more than non existent java or similar coding skills :S

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    In order to return the results without refreshing the page, you'll have to use a JavaScript technique called AJAX to send the information to the PHP script, receive the information it returns and then display it in your popup. Tutorials are a dime a dozen on how to do it. This ought to sent you in the right direction.

    As for what the dude in the other forum said, yes, returning the data in JSON format does make it easier to work with. JSON is JavaScript Object Notation, which is basically just data in the form of key-value pairs in a JavaScript object. I don't think it'd be worth bothering with in this scenario since you're only returning a string. As for AngularJS, I haven't used it myself, but I can tell you that you don't need it for this. jQuery is helpful, because it makes AJAX super easy, so maybe take a look at that as well.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Member #
    39665
    Liked
    1 times
    Hey Ronald,

    Many thanks for your response. I was able to implement exactly what you advised. I was able to use a XMLHttpRequest, send data to the php/server and then retrieve data back from the php and thereby the page did not reload. In my javascript I mentioned my php file , so that the input from the input text could be send there. Everything worked nicely and now I can expand the functionality of my page.

    Thanks again !
    Ronald Roe likes this.

  5. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Member #
    39665
    Liked
    1 times

    Smile

    Ok so now, I need to somehow turn my results into clickable links that will open (make visible) another overlay display and then show that result of that specific result nicely. I have made the php file but when using "id=page.php?id=" it will open the results in a new window and I can't seem to re-use my previously used function as it was "attached to a form" and now on the results I don't have or need a form.

    Is there some way, to use javascript, to write out while the php echos the result into the page, somewhere save or declare a unique variable attached to the result and sending it to the overlay display ?

    The results are something like this :

    Code:
    echo "<a href=page.php?=id",$row['id']," onclick='function();function2();function3'>",$row['tabledata1']," | ",$row['tabledata3']"
    Which will output something like this (Name with link, Surname, Age) :

    Peter Smith | 23

    This is all good and well, but like i said, I need to mangle it in such away that it will

    1.) Forge the results into the link somewhere to be unique and just for that ONE result

    2.) Open the overlay

    3.) Retrieve the unique results previously captured into the link

    4.) Display the advanced results by calling the above, and using the old captured data from the database in regards to the link results

    Kind of similiar to a search you would do on google, you get results, then you click on the result and it will open the page. Only difference is that the data is on mysql and that there are no pages or URLs to parse, everything has to be "generated" for each result when clicked.

    Any help would be greatly appreciated !

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,688
    Member #
    5580
    Liked
    717 times
    I think you need to use PHP SESSION.

    SESSION is sort of like a cookie, but it is retained on the server. It can save variables too. It stays active as long as the user's browser is open.

    As the user goes from page to page (or script to script), each script can read the SESSION and get a copy of the values stored there.

    Your SESSION could be simple as remembering the page the user is currently at. Anytime they move somewhere, from one script to another, each script knows which page they were at.

    That also means you can refresh pages and navigate without needing variables in your URL ... no more of this: myscript.php?id=343

    Google: php session tutorial


  7. #6
    Member DerrickE's Avatar
    Join Date
    Jul 2007
    Location
    Houston, TX
    Posts
    58
    Member #
    15580
    Liked
    10 times
    You could put this into a database. Like have the results or url of the test saved for a specific test. Then when you visit: yoursite.com/results?x=asdf it would grab the saved results.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:26 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com