Jump to content

desperately need help with a form


Go to solution Solved by Barand,

Recommended Posts

Hey guys,

 

As my name refers, I am a beginner at school. I had a project in which I had to make a database. The database contains movies which you can search for. However in the beginning, the results only appeared when the name of the movie was fully written. I solved it by using 'LIKE' in the query. But now I got the problem of results being shown before you searched for the results. The form is on the same file as the result. I could seperate them but in this case, it's no option (for some reasons which doesn't matter). Can this problem get solved in an other way?

 

This is the part of the code of my php file:

 

<form id="form1" name="form1" method="post" action="Website2.php">
 <div id="Zoekend">
  <p>
    <label for="textfield"></label>
    <label for="Zoek"></label>
    <input type="text" name="Zoek" id="Zoek" />
  </p>
  <p>
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_0" />
      Genre</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_1" />
      Naam</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_2" />
      Jaar</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_3" />
      Regisseur</label>
    <br />
    <input type="submit" name="Zoeken" id="Zoeken" value="Zoeken" />
    <br />
  </p>
</form>
</div>
<div id="Resultaat">
<h1> Zoekresultaten: </h1>
<?php
$zoekterm = $_POST['Zoek'];
$username = '1509506_dyon';
$wachtwoord = 'u2pv6stvk';
$host = 'fdb6.awardspace.net';
$database = '1509506_dyon';
mysql_connect($host, $username, $wachtwoord);
mysql_select_db ($database);
$sql = "SELECT * FROM Movies WHERE Genre LIKE '%$zoekterm%'";
$resultaat = mysql_query ($sql);
while ( $row = mysql_fetch_assoc($resultaat) ) {
echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'] .'<br>' ;
}
$sql = "SELECT * FROM Movies WHERE Naam LIKE '%$zoekterm%'";
$resultaat = mysql_query ($sql);
while ( $row = mysql_fetch_assoc($resultaat) ) {
echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'].'<br>' ;
}
$sql = "SELECT * FROM Movies WHERE Jaar LIKE '%$zoekterm%'";
$resultaat = mysql_query ($sql);
while ( $row = mysql_fetch_assoc($resultaat) ) {
echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'].'<br>' ;
}
$sql = "SELECT * FROM Movies WHERE Regisseur LIKE '%$zoekterm%'";
$resultaat = mysql_query ($sql);
while ( $row = mysql_fetch_assoc($resultaat) ) {
echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'] .'<br>';
}
?>
</div>
Link to comment
https://forums.phpfreaks.com/topic/292525-desperately-need-help-with-a-form/
Share on other sites

  • Solution

There are some other things you need to fix.

  • Radio buttons need different values. All yours have a value of "radio"
  • You don't change the search depending on the button selected (not that you could with those values)
  • You are using mysql_xxxx() functions which are about to be removed from PHP.

Try something like this

<html>
<body>
 
<form id="form1" name="form1" method="post" action="">
 <div id="Zoekend">
  <p>
    <label for="Zoek"></label>
    <input type="text" name="Zoek" id="Zoek" />
  </p>
  <p>
    <label>
      <input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" />
      Genre</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1" checked="checked"/>
      Naam</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="3" id="RadioGroup1_2" />
      Jaar</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="4" id="RadioGroup1_3" />
      Regisseur</label>
    <br />
    <input type="submit" name="Zoeken" id="Zoeken" value="Zoeken" />
    <br />
  </p>
 </div>
</form>


<div id="Resultaat">
<h1> Zoekresultaten: </h1>
<?php
//
// if no search attempt then we don't want to process the results
//
if (isset($_POST['Zoek']) && trim($_POST['Zoek'])!='' ) {
    
    $zoekterm = $_POST['Zoek'];
    $username = '1509506_dyon';
    $wachtwoord = '****';                  // deleted for your protection
    $host = 'fdb6.awardspace.net';
    $database = '1509506_dyon';

    $db = new mysqli($host, $username, $wachtwoord, $database);

    $fields = array (
                1 => 'Genre',
                2 => 'Naam',
                3 => 'Jaar',
                4 => 'Regisseur'
                );
    $field = $fields[$_POST['RadioGroup1']];

    $sql = "SELECT * FROM Movies WHERE $field LIKE '%$zoekterm%'";
    $resultaat = $db->query ($sql);
    if ($resultaat->num_rows > 0) {
        while ( $row = $resultaat->fetch_assoc() ) {
            echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'] .'<br>' ;
        }
    }
    else {
        echo "No matching results found";
    }
}
else {
    echo "No search";
}
?>
</div>
</body>
</html>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.