0

Hello, i made this simple code to show info from my database into a table, the form code is below: http://pastebin.com/yyzcjshn

the show.php code is:

       <html>
<head>
<title>Show Result</title>

<?php
$connection = mysql_connect("localhost","root","");
// Check connection
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//select database to use
$db_select = mysql_select_db("sells",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}


$D1 = $_POST['D1'];

//show info
if($D1 != 'Show All'){
$result = mysql_query("SELECT * FROM clients WHERE Status='$D1'", $connection);
}
else $result = mysql_query("SELECT * FROM clients", $connection);
    if(!$result){
die("Database query failed: " . mysql_error());
}
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Client Name</th>
<th>URL</th>
<th>Quantity</th>
<th>Price[$]</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row["Order_ID"]."</td>";
    echo "<td>" . $row["ClientName"]."</td>";
    echo "<td><a href=" . $row['Url'] . " target=_blank >" . $row['Url'] . "</a></td>";
    echo "<td>" . $row["Quantity"]."</td>";
    echo "<td>" . $row["Price"]."</td>";
    echo "<td>" . $row["Status"]."</td>";
    echo "</tr>";
        }
        echo "</table>";



mysql_close($connection);
?>
</head>
</html>

How can i show the result in the same page of the submit form using php (without ajax)?

WhiteOne
  • 887
  • 1
  • 10
  • 20
  • 1
    What is your definition of "same page"? Do you mean without refreshing the browser/navigating to a new page when the submit button is pressed? – EWit Sep 13 '14 at 10:24
  • yes, i don't want it to show the result in another page. – WhiteOne Sep 13 '14 at 10:26
  • 1
    Your code is vulnerable to [injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) and `mysql_*` functions are [deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860046#12860046). – The Blue Dog Sep 13 '14 at 10:31
  • @user3367004 — You can't. What you are asking for is the definition of Ajax. – Quentin Sep 13 '14 at 10:33
  • @Quentin i have make it with ajax: http://pastebin.com/Zrr2wVqW but i see some people use action=" to do it with php, but that doesn't work for me. – WhiteOne Sep 13 '14 at 12:02
  • @TheBlueDog i got this error when i use msqli on my local server "Warning: mysqli_query() expects parameter 1 to be mysqli, string given in .... " any idea how can i solve this problem? thanks – WhiteOne Sep 13 '14 at 12:03

1 Answers1

0

Simple answer: You can't.

In the comments you say people use <?php echo $_SERVER['PHP_SELF']; ?>, but that's not the same. $_SERVER['PHP_SELF'] will echo the current scripts filename. When you use it, you don't have to write the action tag in your form manually.

AJAX is the method you need to update your page without refresh the whole page. You can use the (modern) AJAX method, or a redirect method. Because you don't want to use AJAX here is a redirect method example of your code:

Your form.php. Notice that i replaced your dropdown into a link. You can use links for a easy approuch. You can also keep your dropdown and use Javascript/JQuery to create the right redirection including the GET parameter:

<html>

    <head>
        <title>Show Info In Table</title>
    </head>

    <body>

      ....
      <a href="/form.php?D1=Show All">Show all</a>
      ....
      <a href="/form.php?D1=Finished">Finished</a>
      ....


      <?=include('show.php');?>
    </body>

</html>

show.php, notice that $_POST is replaced with $_GET:

<?php
$connection = mysql_connect("localhost","root","");
// Check connection
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//select database to use
$db_select = mysql_select_db("sells",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}


$D1 = $_GET['D1'];

//show info
if($D1 != 'Show All'){
$result = mysql_query("SELECT * FROM clients WHERE Status='$D1'", $connection);
}
else $result = mysql_query("SELECT * FROM clients", $connection);
    if(!$result){
die("Database query failed: " . mysql_error());
}
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Client Name</th>
<th>URL</th>
<th>Quantity</th>
<th>Price[$]</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row["Order_ID"]."</td>";
    echo "<td>" . $row["ClientName"]."</td>";
    echo "<td><a href=" . $row['Url'] . " target=_blank >" . $row['Url'] . "</a></td>";
    echo "<td>" . $row["Quantity"]."</td>";
    echo "<td>" . $row["Price"]."</td>";
    echo "<td>" . $row["Status"]."</td>";
    echo "</tr>";
        }
        echo "</table>";



mysql_close($connection);
?>

Good luck.

S.Pols
  • 3,414
  • 2
  • 21
  • 42