1

I have small doubt in PHP coding, please help me. Actually I am displaying jobs currently, after searching,the result will displayed in the same page. It is done, but the result is displaying below the content. What I have to do to display only results in that page? I want to make unavailable the previous contents.This is the code:

<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt"  required />
<input type="submit" name="btn" value="search" /><br/>
</form>
</body>
</html>

<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');


 $con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");


$selQ = "Select * from jobpostings";
$res1 = mysqli_query($con, $selQ);

while($row = mysqli_fetch_array($res1))

echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
<br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";


if(isset($_POST["btn"])){

    $query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%') 
or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);

if(mysqli_num_rows($final_results)>0){
    while($results=mysqli_fetch_array($final_results)){
        echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]
                                              <br>".$results[3]."</p>";
    }
  }
 else{
    echo '<b style="color:red;">No results found</b>';
    }
  }
    ?>

Thanks in advance

siva kumar
  • 35
  • 8

4 Answers4

0

Take the whole php code inside <body> and put if conditions to show either of sections. Like this:

<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

<h2>Job Openings</h2>
<?php if(!isset($_POST["btn"]))
{  ?>
  <form method="POST" action="jobopenings.php">
  <input type="text" name="txt"  required />
  <input type="submit" name="btn" value="search" /><br/>
  </form>
<?php }

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');


 $con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");


$selQ = "Select * from jobpostings";
$res1 = mysqli_query($con, $selQ);

while($row = mysqli_fetch_array($res1))

echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
<br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";


if(isset($_POST["btn"])){

    $query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%') 
or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);

if(mysqli_num_rows($final_results)>0){
    while($results=mysqli_fetch_array($final_results)){
        echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]
                                              <br>".$results[3]."</p>";
    }
  }
 else{
    echo '<b style="color:red;">No results found</b>';
    }
  }
    ?>
</body>
</html>
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

Here is the modified code:

<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');


$con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");




$final_results = null;
if(isset($_POST["btn"])){

    $query=$_POST['txt'];
    $query=htmlspecialchars($query);
    $query=mysqli_real_escape_string($con,$query);
    $raw_results="select * from jobpostings where (C_name like '%" .$query."%') or (Job_title like '%".$query."%')";
    $final_results=mysqli_query($con,$raw_results);
}
    ?>

    <html>
<head>
    <title>Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
    <input type="text" name="txt"  required />
    <input type="submit" name="btn" value="search" /><br/>
</form>

<?php

    if(!is_null($final_results) && mysqli_num_rows($final_results)>0){
        while($results=mysqli_fetch_array($final_results)){
            echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]."<br>".$results[3]."</p>";
            }
    }else{
        echo '<b style="color:red;">No results found</b>';
    }

    $selQ = "Select * from jobpostings";
    $res1 = mysqli_query($con, $selQ);

    while($row = mysqli_fetch_array($res1))

        echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
    <br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";
?>

</body>
</html>

I didn't test this code, please confirm it works fine.

Also ALWAYS start php code before <html> tag and everything you have to be print must be within <body> tag

usman ikram
  • 461
  • 4
  • 10
  • Thank you for your response...I got the output what I required by observing your way of code. Thank you sooo much – siva kumar Oct 05 '17 at 05:15
0

1) I would suggest you not to create 2 SQL query (one without search and one with search). You can make one query and append where clause according to the search.

2) "..but the result is displaying below the content." => Wrap all the contents inside <body> tag.

Updated Code

<html>
  <head>
    <title>Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
    <h2>Job Openings</h2>
    <form method="POST" action="jobopenings.php">
      <input type="text" name="txt"  required />
      <input type="submit" name="btn" value="search" /><br/>
    </form>

    <?php
    define('HOST', 'localhost');
    define('USER', 'root');
    define('PASS', '');
    define('DB', '*****');

    $con = mysqli_connect(HOST, USER, PASS, DB) or die("Unable to connect to db");

    $where = "";
    $searched = false;
    if (isset($_POST["btn"]) && isset($_POST['txt'])) {
      $searched = true;
      $txt = htmlspecialchars($_POST['txt']);
      $txt = mysqli_real_escape_string($con, $txt);
      $where = "WHERE (C_name like '%" .$txt. "%') or (Job_title like '%" .$txt. "%')";
    }

    $raw_results = "SELECT * FROM `jobpostings` ".$where;
    $final_results = mysqli_query($con, $raw_results);
    if (mysqli_num_rows($final_results) > 0) {
      while ($results = mysqli_fetch_array($final_results)) {
        if(!$searched){
          echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]."<br>".$results[3]."</p>";
        } else {
          echo $results[3] . "<br> Job Id:<b>" . $results[2] . "</b><br><b>" . $results[1] . "</b>" . "<br>" . "Exp:" . $results[5] . "<br>" . "Location:" . $results[6] . "<br>" . $results[8] . "<br><br>";
        }
      }
    } else {
      echo '<b style="color:red;">No results found</b>';
    }?>
  </body>
</html>

Note: Use Prepared Statements to avoid SQL Injections

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt"  required />
<input type="submit" name="btn" value="search" /><br/>
</form>
<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');

$con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db"); 

if(isset($_POST["btn"])){

$query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%') 
                                       or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);

if(mysqli_num_rows($final_results)>0){
while($results=mysqli_fetch_array($final_results)){
    echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]
                                          <br>".$results[3]."</p>";
   }
  }
 else{
  echo '<b style="color:red;">No results found</b>';
  }
 }
  else{
  $selQ = "Select * from jobpostings";
   $res1 = mysqli_query($con, $selQ);

  while($row = mysqli_fetch_array($res1))

  echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"." 
 <br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";}
 ?>
 </body>
 </html>
siva kumar
  • 35
  • 8