www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP reloads page

Hybrid View

  1. #1
    Join Date
    Jul 2014
    Posts
    4

    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. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    715
    If you want that 'button' to only trigger your js function, change it from a submit to a button type. Or if that won't work, you could use a 'return' parameter on the onclick so that your js function can determine whether to perform the submit something like this:
    PHP Code:
    <input type='submit' value='Submit' onclick='return doSomething()'
    Your doSomething function can reset the display of your element and also return either 'true' or 'false' in order to make the submit happen or not.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    Jul 2014
    Posts
    4
    Just a quick update,

    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 it could be send there.

    But thanks for the help

  4. #4
    Join Date
    Jul 2014
    Posts
    4
    I have a search page, that returns mysql query like so :

    Code:
    while($row=mysqli_fetch_array($result))
    {
    echo "<a onclick='function1();function2();function3()'>",$row['compn'],"</a>";
    echo "<form><input type='text' id='tester' value='",$row['compuid'],"' hidden='hidden'></input></form>";
    }
    Nothing on my page opens a new page or loads a new one. Everything basically resides on the same page. The results are displayed in another overlay :

    Code:
    <a onclick="function1();function2();function3()"></a>
    <form><input id="tester" type="text" hidden="hidden" value="abcdefg"></input></form>
    
    <a onclick="function1();function2();function3()"></a>
    <form><input id="tester" type="text" hidden="hidden" value="mnopqrs"></input></form>
    As you can see above, it kind of works BUT not like I want it to be :
    If clicked on the top "link", it will load id="tester" into
    Code:
    document.getElementById("tester").value
    ; and then display abcdefg. If I click on the bottom link, it will again load tester and display abcdefg. HOW do I get the code or the php to load a dynamic attribute or something, so that if I click the below link, it will actually display mnopqrs ?

    Note: When a result link is clicked, it should display more details on an overlay by reading off from the php. For example :

    link 1 = Peter, LAW
    link 2 = Kate, JOURNALISM

    (click on link 1
    Peter, BIO : Peter has been studying LAW for more than 2 years,
    Peter was born on 24/05/1992. Picture of Peter


    (click on link 2
    Kate, BIO : Kate has been studying ...

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    715
    Sorry - your code is too broken down to make sense of. Top link? Bottom link? Two different chunks of code and I don't know what you are trying to show. I do know that if you have two elements with the same id you're going to have a js problem.

    And - why do you use input tags with 'type=text' and then add a hidden attribute? Why not just 'type=hidden'?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  6. #6
    Join Date
    Jul 2014
    Posts
    4
    The code works fine on my side, maybe I only gave SOME of the code to only give an idea what I would like to achieve ? I chose hidden because that's... I only have 2 weeks experience, so FTLOG bear with me

    Here is a scaled down, hopefully understandable, simple, algorithm/idea/FICTION code of what I am aiming for :

    First output generated from search results :

    microsoft | citynorth (hidden from view : [id="1"]) *These id's are the actual IDs from the mysql primary key
    microsoft | cityeast (hidden from view : [id="2"])
    apple | citycentre (hidden from view : [id="3"])
    nvidia | citynorth (hidden from view : [id="4"])
    Pete's cool IT shop | citysouth (hidden from view : [id="5"])

    Code:
    var k = some mystical javascript function to "get id clicked" above
    Code:
    $m = select * from abc where id == k
    $l = mysqli_query(m, x)
    Code:
     while($row=mysqli_fetch_array($l))
    		{
    			echo $row[item1]," | ",$row[item2]," | "$row[item3]... 
    		}
    Example : Let's kindly assume that user clicks "microsoft | citynorth"
    the only output would then be :

    microsoft | citynorth | MS north has sold over 100000 copies of MSKPLQR | 25 | 100000 | PICTURE OF VENUE | reseller

    __________________________________
    #NOTE: here the view explained :
    1.) Items in bold, are comments
    2.) Items in italic, are on screen display, that the user sees
    3.) code = code/algorithm/php/javascript w/e


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles