Page 1 of 1

please help

Posted: Mon Feb 16, 2009 11:02 am
by allan16
i am developing a music website and here is the issue:

one of the HTML pages its called genre.html, so I have a list/menu that displays all genre types available for users to choose. The user will select the genre option and will click on the search option.

There is a mySQL database with one table with different information. One of the fileds its called "genre".
The code that I have right now is that when I select the first option which is rock it gives the result on another page different of the one that i am one. I need this result to show up on the same html page. And also how could I do it for the other genre types?

HTML:

Code: Select all

<form name="form1" method="post" action="php_scripts/rock.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>
PHP:

Code: Select all

$con = mysql_connect("xxxxxxx","xxxxxx","xxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("xxxxxxxx", $con);
 
$result = mysql_query("SELECT * FROM artist WHERE genre='Rock'");
 
$num_rows = mysql_num_rows($result);
print "<font color='#990000' FACE='Tahoma' FONT-SIZE:'11px'>There are $num_rows records.</font><P>";
 
 
   while($row = mysql_fetch_array($result))
  { 
  
  print "Artista: ";
  echo $row['artista'];
  echo "<br />";
  
  print "Contacto: ";
  echo $row['contacto'];
  echo "<br />";
  
    print "Oficina: ";
  echo $row['oficina'];
  echo "<br />";
  
    print "Celular: ";
  echo $row['celular'];
  echo "<br />";
  
    print "Direccion: ";
  echo $row['direccion'];
  echo "<br />";
  
    print "Pagina Web: ";
  echo $row['paginaweb'];
  echo "<br />";
  
    print "E-mail: ";
  echo $row['email'];
  echo "<br />";
  
    print "Genero: ";
  echo $row['genero'];
  echo "<br /><br />";
   
    }
 
mysql_close($con);
?>

Re: please help

Posted: Mon Feb 16, 2009 11:25 am
by lostprophetpunk
You just need to put it in one PHP page like below...

Code: Select all

<?php
 
if(!$_POST['Submit']){
       <form name="form1" method="post" action="php_scripts/rock.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>
}else {
 
$con = mysql_connect("xxxxxxx","xxxxxx","xxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("xxxxxxxx", $con);
 
$result = mysql_query("SELECT * FROM artist WHERE genre='Rock'");
 
$num_rows = mysql_num_rows($result);
print "<font color='#990000' FACE='Tahoma' FONT-SIZE:'11px'>There are $num_rows records.</font><P>";
 
 
   while($row = mysql_fetch_array($result))
  { 
  
  print "Artista: ";
  echo $row['artista'];
  echo "<br />";
  
  print "Contacto: ";
  echo $row['contacto'];
  echo "<br />";
  
    print "Oficina: ";
  echo $row['oficina'];
  echo "<br />";
  
    print "Celular: ";
  echo $row['celular'];
  echo "<br />";
  
    print "Direccion: ";
  echo $row['direccion'];
  echo "<br />";
  
    print "Pagina Web: ";
  echo $row['paginaweb'];
  echo "<br />";
  
    print "E-mail: ";
  echo $row['email'];
  echo "<br />";
  
    print "Genero: ";
  echo $row['genero'];
  echo "<br /><br />";
   
    }
 
mysql_close($con);
 
}
 
?>
Let me take you through it...

The following code checks to see whether the user has submitted the form, but the name of the submit button must go between the '...' otherwise it won't work...

Code: Select all

if(!$_POST['Submit']){
After the '{' bracket, any code placed after this will be executed when the user submits the form, such as sort of an exmaple here...

Code: Select all

else{
//your connection and other code here
echo $somevariable //do not include this as it was an example line of code
}
You can also display the form again (just place it in the curly brackets after 'else'.

I hope that was clear as I am usually rubbish at explaining things.

Re: please help

Posted: Mon Feb 16, 2009 12:04 pm
by allan16
ok,

currently I have 3 files. Genre.html, rock.php and new.php. Do i still need rock.php? Is this ok ?

NEW.php:

Code: Select all

<?php
 
if(!$_POST['Submit']){
       <form name="form1" method="post" action="php_scripts/rock.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>
}else {
 
       <form name="form1" method="post" action="php_scripts/rock.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>
 
 }
$con = mysql_connect("xxxxxxx","xxxxxxxx","xxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("xxxx", $con);
 
$result = mysql_query("SELECT * FROM artist WHERE genre='Rock'");
 
$num_rows = mysql_num_rows($result);
print "<font color='#990000' FACE='Tahoma' FONT-SIZE:'11px'>There are $num_rows records.</font><P>";
 
 
   while($row = mysql_fetch_array($result))
  {
 
  print "Artista: ";
  echo $row['artista'];
  echo "<br />";
 
  print "Contacto: ";
  echo $row['contacto'];
  echo "<br />";
 
    print "Oficina: ";
  echo $row['oficina'];
  echo "<br />";
 
    print "Celular: ";
  echo $row['celular'];
  echo "<br />";
 
    print "Direccion: ";
  echo $row['direccion'];
  echo "<br />";
 
    print "Pagina Web: ";
  echo $row['paginaweb'];
  echo "<br />";
 
    print "E-mail: ";
  echo $row['email'];
  echo "<br />";
 
    print "Genero: ";
  echo $row['genero'];
  echo "<br /><br />";
   
    }
 
mysql_close($con);
 
}
 
?>
rock.php

Code: Select all

<?php
 
 
 
$con = mysql_connect("xxxxxxxx","xxxxxxx","xxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("xxxxxx", $con);
 
$result = mysql_query("SELECT * FROM artist WHERE genre='Rock'");
 
$num_rows = mysql_num_rows($result);
print "<font color='#990000' FACE='Tahoma' FONT-SIZE:'11px'>There are $num_rows records.</font><P>";
 
 
   while($row = mysql_fetch_array($result))
  { 
  
  print "Artista: ";
  echo $row['artista'];
  echo "<br />";
  
  print "Contacto: ";
  echo $row['contacto'];
  echo "<br />";
  
    print "Oficina: ";
  echo $row['oficina'];
  echo "<br />";
  
    print "Celular: ";
  echo $row['celular'];
  echo "<br />";
  
    print "Direccion: ";
  echo $row['direccion'];
  echo "<br />";
  
    print "Pagina Web: ";
  echo $row['paginaweb'];
  echo "<br />";
  
    print "E-mail: ";
  echo $row['email'];
  echo "<br />";
  
    print "Genero: ";
  echo $row['genero'];
  echo "<br /><br />";
   
    }
 
mysql_close($con);
?>
NEW.html

Code: Select all

 
     <form name="form1" method="post" action="php_scripts/new.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>

Re: please help

Posted: Mon Feb 16, 2009 12:14 pm
by lostprophetpunk
No, you do not need the HTML file at all, you just need the 'new.php'. I sort of left some stuff out of the one I posted earlier, here is the corrected version...

Code: Select all

<?php
 
if(!$_POST['Submit']){
?>
       <form name="form1" method="post" action="php_scripts/rock.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>
<?php
}else {
?> 
       <form name="form1" method="post" action="php_scripts/rock.php">
          <label><br>
          Genero:  
            <select name="select">
              <option value=Rock>Rock & Roll </option>
              <option value=Pop>Pop</option>
              <option>Alternativo</option>
              <option>Electronico</option>
              <option>Salsa</option>
            </select>
          </label>
          <br>
          <br>
          <label>
          <input type="submit" name="Submit" value="Buscar">
          </label>
        </form>
<?php 
 }
$con = mysql_connect("xxxxxxx","xxxxxxxx","xxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("xxxx", $con);
 
$result = mysql_query("SELECT * FROM artist WHERE genre='Rock'");
 
$num_rows = mysql_num_rows($result);
print "<font color='#990000' FACE='Tahoma' FONT-SIZE:'11px'>There are $num_rows records.</font><P>";
 
 
   while($row = mysql_fetch_array($result))
  {
 
  print "Artista: ";
  echo $row['artista'];
  echo "<br />";
 
  print "Contacto: ";
  echo $row['contacto'];
  echo "<br />";
 
    print "Oficina: ";
  echo $row['oficina'];
  echo "<br />";
 
    print "Celular: ";
  echo $row['celular'];
  echo "<br />";
 
    print "Direccion: ";
  echo $row['direccion'];
  echo "<br />";
 
    print "Pagina Web: ";
  echo $row['paginaweb'];
  echo "<br />";
 
    print "E-mail: ";
  echo $row['email'];
  echo "<br />";
 
    print "Genero: ";
  echo $row['genero'];
  echo "<br /><br />";
   
    }
 
mysql_close($con);
 
}
 
?>

Re: please help

Posted: Mon Feb 16, 2009 12:17 pm
by allan16
Ok and besides i would need all the code on the html file on this NEW.php file, correct so it would look the same as the html file