1

I have a registration form, and what I like to so is to use a mySQL query to retrieve data in to the form input fields.

this is part of the very long form:

<form class="form-horizontal" id="registration" method='post' action='ConnectDB.php' onsubmit='return ValidateForm(this);'>
    <fieldset>
        <h1 style="margin-top: px;float:right; color:#62A1D5"><br><b>registration</b></h1>
        <table id="TBackGround">
            <td style="padding-left:10px; padding-right:10px;">
                <p id="pd"  style="margin-top:5px; margin-right:2px; font-size:16px; text-decoration:underline"><b><br>details:</b><p> 
                <p style="line-height: 1.3em" class="details">
                <div class="control-group"> 
                    <label class="control-label">ID </label>
                    <input type="text"name="studentID"  align= "right" class="textbox" ><span id="errorID"></span> <br/>  
                </div>
                <div class="control-group"> 
                    <label class="control-label" >First name</label>
                    <input type="text" name="Fname"  class="textbox" ><span id="errorFirstName"> <br/>
                </div>
</form>

How do I set the retrived data to be loaded in to the form's input fields? What I have is a query to retrieve the ID of the record but I don't know how to set the entire query result on to the fields.

my php query:

<?php 

if(isset($_POST['submit']))
{
$query = $_POST['query']; 
$min_length = 1;
if(strlen($query) >= $min_length)
{ 
$query = htmlspecialchars($query); 
$query = mysql_real_escape_string($query); 
echo "<table border='0' width='300' align='center' cellpadding='1' cellspacing='1'>";
echo "<tr align='center' bgcolor='#002C40'> 
?>
<td height='35px' width='150px'>id</td> <td>first name</td>

</tr>"; 
$raw_results = 

mysql_query("SELECT * FROM student WHERE (`idStudent` LIKE '%".$query."%') OR (`FirstName` LIKE '%".$query."%')"); 
if(mysql_num_rows($raw_results) > 0)
{
while($results = mysql_fetch_array($raw_results))
{ 

echo "<tr align='center' bgcolor='#0f7ea3'>

 <td height='25px'> "
 .$results['idStudent']."</td> <td>".$results['FirstName']."</td>






</tr>" ;
}

}
else{ 
echo "<tr align='center' bgcolor='#6C0000'>

<td colspan='2' height='25px'>No results</td><tr>"; 
echo "</table>"; 
} 
}
else{ 
echo "Minimum length is ".$min_length;
}} 

?>
5
  • you mean: suggestion, auto-complete? Than you need AJAX. Commented Apr 16, 2014 at 14:29
  • Can I get an example, even a very simple one? Commented Apr 16, 2014 at 14:30
  • @user2674835: what you mean? Example of suggestion, auto-complete? Commented Apr 16, 2014 at 14:34
  • an example of using a simple select query and loading the data in to input fields Commented Apr 16, 2014 at 14:37
  • 1
    it would be very helpful if you included only the relevant code portions Commented Apr 16, 2014 at 14:40

2 Answers 2

1

You need to output layout of your form from php file. Here is small example can help you.

File register.php

if (!isset($_POST['submit'])) {
    mysql_connect();
    $res = mysql_query("SELECT * FROM users WHERE user_id=1");
    $user = array_map('htmlspecialchars', mysql_fetch_assoc($res));

    echo <<<CUT
<form action="register.php" method="post" >
    <lable>Name:</label> <input type="text" name="name" value="{$user['name']}" />
    <lable>Country:</label> <input type="text" name="name" value="{$user['country']}" />
    <input type="submit" name="submit" value="Submit" />
</form>
CUT;
} else {
    //handle submited data here
}
Sign up to request clarification or add additional context in comments.

Comments

0

OK, Basics!

You've an form with input-elements. By sending the form to the server, the server-side-script can get the values of the form-elements.

Look this:

<form action="the_script_location.php" method="post">
<input type="text" name="element1" />
<input type="submit" name="send" />
</form>

On the server-side-script you can now get the values from form like this:

<?php
if( isset( $_POST[ 'send' ] ) ){
    $var = $_POST[ 'element1' ];
    //now you can use the var for an query to your database
    //please note: this very basic, without any security of injection
    $res = mysql_query( 'SELECT `any` FROM `anyTable` WHERE `any` LIKE \'%'.$var.'%\'' );
    if( mysql_num_row( $res ){
        $row = mysql_fetch_assoc( $res ); //get one (first) result
    }
}
?>

Now you can update the form:

<form action="the_script_location.php" method="post">
<input type="text" name="element1" value="<?php isset( $row[ 'your_filed_in_database' ] ) ? $row[ 'your_filed_in_database' ] : '' ?>" />
<input type="submit" name="send" />
</form>

1 Comment

Thanks for the example! However, I'm still stuck on understanding how to pass the query results to my form. shouldn't be a code for opening my desired page after the php code is done?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.