2

I'm making something like stock-price finder, there is data table made up of stock name, code, price. enter image description here

what I want to do is if user input stock name on index.html, the result will shown at result.php , and If user click stock name, describe more information on view.php. anyway I made up this, the problem is... I think the pagination would be complicated with user, I want to combine pages to one page. not using result.php and view.php, but only index.html.

enter image description here

Like this...

enter image description here

There are some variable moving page to page, so I'm very afraid of how can I merge this pages. user input text, and the code. I don't know how I can control it.

index.html

    <form action="./result.php" method="get">
    <label> 
        Search
        <input type="text" name="keywords">
        <input type="submit" value="Search">
    </label>
    </form>

result.php

<?php
require_once './require/db.php';
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("SELECT name,code FROM data2 WHERE name LIKE '%{$keywords}%' ");
?>

<div class="result-count">
    Found <?php echo $query->num_rows; ?> results. 
</div>

<?php
}
if($query->num_rows) {
    while($r = $query->fetch_object()) {
    ?>

        <div class="result">
            <a href="./result.php?code=<?php echo $r->code;?>">
<?php echo $r->name; ?></a>
        </div>

        <br />
        <br />

<?php 
}
}
?>

view.php

<?php
require_once("./require/file_get_contents_curl.php");
$code = $_GET['code'];
$source = file_get_contents("http://www.nasdaq.com/symbol/$code");
$dom = new DOMDocument();
@$dom->loadHTML();
$stacks =  $dom->getElementsByTagName('dl')->item(0)->textContent;

?>

The above code I made up. but I want to merge it. How can I combine 3 documents to 1 document? because of user input 'keywords(index.html->result.php)' and '_GET variable(result.php->view.php)' It's very difficult to me. Please help me.

ton1
  • 7,238
  • 18
  • 71
  • 126

2 Answers2

3

You can keep your separate files, but instead of displaying the information inside those actual files, you can call them via AJAX (asynchronous HTTP requests) instead. Here's the syntax using jQuery:

index.html

<input type="text" id="keywords">
<button id="search">Search</button>

<div id="result"></div> <!-- The div that will be filled with the result from the AJAX request -->

Javascript

$('#search').click(function(){
   $.ajax({
      url: "result.php",
      method: "POST",
      data: {keywords: $('#keywords').val()}
    }).success(function(result) {
      $('#result').html(result);
    });
});

result.php

<?php
require_once './require/db.php';
if(isset($_POST['keywords'])){
$keywords = $db->escape_string($_POST['keywords']);
$query = $db->query("SELECT name,code FROM data2 WHERE name LIKE ' {$keywords}%' ");

echo "Found ".$query->num_rows." results."; 

?>

So in the example I made up above you pass in the variable keywords as keywords. This means that inside result.php, you can access the variable keywords via $_POST['keywords'].

So basically, you do the exact same things in result.php and view.php you've done so far, but you return the data to the index.html file instead of just echoing it out in that file. If you want to read more about the AJAX function in jQuery, here's a link: jQuery.ajax().

Hope this helps, please let me know if you wonder something.

Gustaf Gunér
  • 2,272
  • 4
  • 17
  • 23
1

This is a very broad question.

So the answer will be very subjective.

If this is your first rodeo, I think its a good time to learn about AJAX. The basic idea is that you let a javascript request information from the server, and add the response to the page.

AJAX is a complex set of methods, but with modern frameworks like jquery, it's become easy to implement.

See this documentation: http://www.w3schools.com/jquery/jquery_get_started.asp

and https://api.jquery.com/jquery.get/

when jquery is added to your page, try this:

$.get( "result.php", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

if you do not like ajax, try adding iframes to the index.html, and set the src of the iframe to the php page you want to display. This will cause them to load once. So you need to refresh them everytime the dropdown changes.

that could be a little tricky: How to refresh an IFrame using Javascript?

Community
  • 1
  • 1
Henrik
  • 2,180
  • 16
  • 29