javascript - Import a variable from one page to another in PHP -


im making autocomplete search box lists different courses, , im trying use search button next display results of selected course on page using sql database. page looks this: index.php

so whatever choice pick autocomplete, want "search courses" button link page , display results of selected course in search box page before. these results pulled sql database.

im having trouble linking , can't figure out.

this search box , button:

<div class="row">     <div class="ui-widget col-md-4 col-md-offset-4">     <label for="tags">search: </label>     <input id="tags" type="search">     <input type="submit" value="search courses">     </div> </div> 

do need set sort of variable pass on next page?

appreciate help, thanks.

this autocomplete.php file:

<?php $mysqli = new mysqli("localhost", "scott", "tiger", "courses"); $term = mysqli_real_escape_string($mysqli,$_request['term']); $query = " select title   course  title '$term%'  order title"; $return = array(); $result = $mysqli->query($query); while ($row = $result->fetch_array()){     array_push($return,$row[0]); } echo json_encode($return); ?> 

index.html:

<!doctype html> <html lang="en"> <head>   <meta charset="utf-8">   <title>edinburgh napier university</title>   <div class="row">     <div class="col-md-4 col-md-offset-4">     <img id="napierlogo" src="logo.jpg"/>     </div>   </div>        <meta name="viewport" content="width=device-width, initial-scale=1">   <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">    <script src="//code.jquery.com/jquery-1.10.2.js"></script>   <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>   <link rel="stylesheet" href="/resources/demos/style.css"> <script> $(function() { $( "#tags" ).autocomplete({     source: "autocomplete.php",     autofocus:true }); }); </script>  <script>   $(function() {     $( "#menu" ).menu();   });   </script>   <style>   .ui-menu { width: 150px; }   </style>    <script>   $(function() {     $( "input[type=submit]" )       .button()       .click(function( event ) {         event.preventdefault();       });   });   </script>    <?php     isset($_get['res'])?$var=mysql_escape_string($_get['res']):$res='';   ?>  </head> <body>  <p></p>      <div class="row">     <div class="ui-widget col-md-4 col-md-offset-4">     <label for="tags">search: </label>     <input id="tags" type="search">     <input type="submit" value="search courses">     </div> </div>  <p></p>  <ul id="menu">   <li><a href="http://socwebtech1.napier.ac.uk/~40171342/course/index.php">search</li>   <li><a href="http://my.napier.ac.uk/pages/home.aspx">mynapier</a></li>   <li>contact     <ul>       <li><a href="https://www.facebook.com/edinburghnapieruniversity/">facebook</li>       <li><a href="https://twitter.com/edinburghnapier?  ref_src=twsrc%5egoogle%7ctwcamp%5eserp%7ctwgr%5eauthor">twitter</li>       <li><a href="https://uk.linkedin.com/edu/edinburgh-napier-university-12617">linkedin</li>     </ul>   </li>   </ul>  </body> </html> 

first, if it's not already, index page needs .php file instead of .html.

second, need modify search box form submits when click search button. send post request results.php page can load results database using search term entered in textbox.

your textbox html code should become:

<div class="row">     <form action="results.php" method="post">     <div class="ui-widget col-md-4 col-md-offset-4">         <label for="tags">search: </label>         <input id="tags" type="search" name="search">         <!-- added 'name' attribute ^ here              we'll need later in php file -->         <input type="submit" value="search courses">     </div>     </form> </div> 

and results.php should have php code @ top similar this:

<?php  $searchterm = $_post["search"]; // key part    ->     ^    ^ // needs match name of search box in html  // todo -- sanitize input (never trust user input) // using mysqli_real_escape_string() or similar. // use perform search  // testing: echo $searchterm; 

more information on $_post available here: http://php.net/manual/en/reserved.variables.post.php

as side note, should using pdo database access. has load of great features, including "prepared statements" prevent against sql injections. that, , it's best practise , it's future maintainability.

more info here: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -