1

I'm currently in the process of coding a MySQL search page and I'm unable to work out how to make it so that if there's no data put in one form it'll do nothing, and if there's data in the other form it'll search the database for that value.

<?PHP 
echo '<h3 class="hp-1">Kick Logs</h3><div class="wrapper">
<div class="logcol-1"><form name="form1" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '">
<input name="name" type="text" id="name" placeholder="Players Name"> 
</form>';
echo '<form name="form2" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '"><input name="reason" type="text" id="reason" placeholder="Kick Reason"></form>';
$name = mysql_real_escape_string($name);
$reason = mysql_real_escape_string($reason);

$kicklogname = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$name%'") or die(mysql_error());
$kicklogreason = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$reason%'") or die(mysql_error());
if($name == ""){
echo "You must enter a name to search"; }
else {
    echo '<table width="700" border="0">
        <tr class="listheader">
        <td width="100" bgcolor="#afe6ff">Username</td>
        <td width="220" bgcolor="#afe6ff">Reason</td>
    </tr>';
    while($row = mysql_fetch_array($kicklogname))
    {
        echo '<tr><td bgcolor="#daf4ff" class="contentleft">';
        echo $row['user'];
        echo '</td><td bgcolor="#eefaff" class="contentright">';
        echo $row['reason'];
        echo '</td></tr>';
    }
    echo '</table></div></div>';
}
?>
putvande
  • 15,068
  • 3
  • 34
  • 50
  • Ever heard of $_POST? – Mihai Aug 28 '13 at 14:47
  • [Why should u not use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – DarkBee Aug 28 '13 at 14:49
  • A few tips: - Use MYSQLi instead of MYSQL - You don't have to have those four lines of HTML inside `echo`, just put it before the PHP opening tag, and - You don't have to put `$_SERVER['REQUEST_URI']` in form's action. Don't even define `action` attribute if the form is supposed to send itself to the exact same page. – Smuuf Aug 28 '13 at 14:51
  • Do you mean that the form fields must be updated dynamically? Please explain a little the workflow you want to achieve. – Diego Fernández Durán Aug 28 '13 at 15:14

1 Answers1

0

Update

Sorry, re-read the question. To check multiple forms on the same page you need to add a submit button to each form and give it a name:

<?php
if (!empty($_POST) && isset($_POST['reasonsubmit'])) {
    echo 'Submitted reason form';
}
if (!empty($_POST) && isset($_POST['namesubmit'])) {
    echo 'Submitted name form';
}
?>

<form action="reason.php" method="post">
    <input type="text" name="reason" id="reason" />
    <input type="submit" name="reasonsubmit" />
</form>
<form action="name.php" method="post">
    <input type="text" name="name" id="name" />
    <input type="submit" name="namesubmit" />
</form>

Alternatively, if you didn't want to give the submit button a name you can just add a hidden field to each form and you will get the same behavior.

<form action="reason.php" method="post">
    <input type="hidden" name="reasonsubmit" id="reasonsubmit" />
    <input type="text" name="reason" id="reason" />
    <input type="submit" />
</form>
<form action="name.php" method="post">
    <input type="hidden" name="namesubmit" id="namesubmit" />
    <input type="text" name="name" id="name" />
    <input type="submit" />
</form>
SeanWM
  • 16,789
  • 7
  • 51
  • 83