khefner Posted December 26, 2007 Share Posted December 26, 2007 Hello I am trying to do something pretty basic (I think!) but I am stuck on one part. Basically, I want to use a text input box to allow the user to enter a number. This number is then used to retrieve a record from the database. I have everything (connect to the database, manually retrieve some data etc) working except I dont know how to use the value from the input text box. I have a submit button but I dont know how to tie that in to the PHP. Here is the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> PDF FINDER <br><br> <?php echo " <b>Enter number: </b><input type='text' name='roll_frame'><br><br>"; echo " <align='center'><input type='submit' value='retrieve_pdf'><br><br>"; //Connect To Database $hostname=''; $username='retrieve'; $password=''; $dbname='retrieve'; mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); $query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='$roll_frame'") or die(mysql_error()); /*$query_pdf = mysql_query("SELECT * FROM data") or die(mysql_error()); */ $info = mysql_fetch_array( $query_pdf ); Print "<b>pdf:</b> ".$info['pdf_link'] . " "; /*Print "<b>roll frame:</b> ".$info['roll_frame'] . " <br>"; */ ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/ Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 In order to process the data you should have some logic to determine if submit was pressed or not. If it was, go to your PHP code, if it wasn't, display the form. Also, use a FORM. Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423098 Share on other sites More sharing options...
drummer101 Posted December 26, 2007 Share Posted December 26, 2007 @$submit = $_POST/GET['retrieve_pdf'] if(!isset($submit)){ <form here> } else { <sql here> } Edit: I don't know if you accidentally edited it out somewhere or not, but $roll_frame isn't defined anywhere in the code you posted Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423133 Share on other sites More sharing options...
dewey_witt Posted December 26, 2007 Share Posted December 26, 2007 Try this instead. <?php ("SELECT pdf_link FROM data WHERE roll_frame ='". $roll_frame ."'") ?> Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423154 Share on other sites More sharing options...
khefner Posted December 26, 2007 Author Share Posted December 26, 2007 I see now that I need to define the form - what would the action be? Thanks <form method='post' action='something here'>"; Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423178 Share on other sites More sharing options...
Ken2k7 Posted December 26, 2007 Share Posted December 26, 2007 The action is the file you want the data to be submitted into. Looking at your code, you probably want it to submit the data to the same page since that's where you have your SQL calls. So put something like $_SERVER['PHP_SELF'] echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>"; Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423187 Share on other sites More sharing options...
trq Posted December 26, 2007 Share Posted December 26, 2007 If the form is to post to itself, you do not need to define an action at all. echo "<form method='post'>"; Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423199 Share on other sites More sharing options...
khefner Posted December 26, 2007 Author Share Posted December 26, 2007 Well I used all the advice from the various posts as best I could and came up with this: (See below) The page displays, but when I enter a valid number and hit the submit button, the page refreshes but the database info is not there. I think one problem is that my if and else are not in the right locations? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>PDF Retrieve Using Roll and Frame Number</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <div align="center">PDF FINDER <br> <br> <?php $submit = $_REQUEST['roll_frame'] ; if(!isset($submit)){ //Connect To Database $hostname=''; $username='retrieve'; $password=''; $dbname='retrieve'; mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); $query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='roll_frame'") or die(mysql_error()); $info = mysql_fetch_array( $query_pdf ); Print "<b>pdf:</b> ".$info['pdf_link'] . " "; } else { echo "<form method='post'>"; echo " <b>Enter Roll and Frame number: </b><input type='text' name='roll_frame'><br><br>"; echo " <align='center'><input type='submit' value='retrieve pdf'><br><br>"; } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423255 Share on other sites More sharing options...
Ken2k7 Posted December 26, 2007 Share Posted December 26, 2007 Please use code tags. Some things you may want to consider: 1. You should check if the form is submitted. To do that, you need to put a name attribute to the submit button and in the PHP, add this: <?php if ($_POST['submit']){ // the 'submit' here is the name you give to your submit button // do something; form is submitted } else { // do something else; form is not submitted } ?> 2. In this line: <?php $query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='roll_frame'") or die(mysql_error()); ?> When you say "WHERE roll_frame='roll_frame'", what exactly do you want to do here? You want to see where roll_frame has the value 'roll_frame'? 3. Watch your HTML tags. You need to close them properly. Close off form. Also, <align='center'> isn't proper HTML. Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423261 Share on other sites More sharing options...
suma237 Posted December 26, 2007 Share Posted December 26, 2007 1.correct submit button as <input type='submit' value='retrieve pdf' name='submit1'> 2.Use if codition as if(isset($_POST['submit1'])) { //conditon } Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423282 Share on other sites More sharing options...
khefner Posted December 26, 2007 Author Share Posted December 26, 2007 Please use code tags. Some things you may want to consider: 1. You should check if the form is submitted. To do that, you need to put a name attribute to the submit button and in the PHP, add this: <?php if ($_POST['submit']){ // the 'submit' here is the name you give to your submit button // do something; form is submitted } else { // do something else; form is not submitted } ?> 2. In this line: <?php $query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='roll_frame'") or die(mysql_error()); ?> When you say "WHERE roll_frame='roll_frame'", what exactly do you want to do here? You want to see where roll_frame has the value 'roll_frame'? 3. Watch your HTML tags. You need to close them properly. Close off form. Also, <align='center'> isn't proper HTML. Thanks for the help regarding the "WHERE roll_frame='roll_frame'" What I am trying to do is take the value entered in the input box 'roll_frame', and use it to find a match in the database field called roll_frame. Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423424 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 The variable would be contained in $_POST['roll_frame'] or $_GET['roll_frame'], depending on what METHOD you used in your FORM. Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-423431 Share on other sites More sharing options...
khefner Posted December 30, 2007 Author Share Posted December 30, 2007 just wanted to say thanks to everyone for the help. I have the script working now. Regards, Kevin Quote Link to comment https://forums.phpfreaks.com/topic/83180-solved-using-a-input-box-to-retrieve-data-from-mysql-database/#findComment-425944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.