Jump to content

Pulling values from the database and putting them into forms?


vomitbomb

Recommended Posts

You would do something like this:

 

<?php
//include the connections
include 'db.php';
//make the proper query "Safely" (this is just simple for time sake
$sql = mysql_query("SELECT * FROM tableName WHERE id = '{$_GET['id']}'");

if(mysql_num_rows($sql) > 0){
     $row = mysql_fetch_array($sql);
     echo'<form action="#" method="post">
     <input type="text" name="text1" value="'.$row['column1'].'" />
     <input type="submit" value="SEND!" />
     </form>';
}else{
     echo 'Invalid ID';
}
?>

Link to comment
Share on other sites

ah k I get what you're explaining, nah I need this to send this to another document.

 

$fname = $_POST['fname'];
echo $fname;

echo '<label>First
<input type="text" name="fname" id="fname" value="$fname"/>
</label>'

 

Ok the echo up there gives me the right information, although on this particular form it just leaves $fname instead of it's value. I've also tried value=' echo $fname; '>

Link to comment
Share on other sites

Cool I managed to get the value into the form, but now I can't reset the form because the value is set to $fname.  :-\

 

<input type="text" name="fname" id="fname" value='<? echo $fname;?>'>

and the reset:

<input type="reset" value="Reset Fields" />

 

Any idea on how I can reset this?

Link to comment
Share on other sites

function resetFields(){
     var inputs = document.getElementsByTagName('input');
     for(var i=0;i < inputs.length;i++){
          inputs[i].value = '';
     }
}

am I meant to be changing something there? causes an error on the second line.

Link to comment
Share on other sites

OK, with the reset button, it just sets it to its original state, you have to use a button to do this:

 

<script type="text/javascript">
function resetFields(){
     var inputs = document.getElementsByTagName('input');
     for(var i=0;i < inputs.length;i++){
	if(inputs[i].getAttribute("type") == "text"){
		inputs[i].value = '';
	}
     }
}
</script>



<form name="form1">
<input type="text" value="cat" />
<input type="button" value="Clear" onclick="resetFields()" />
</form>

Link to comment
Share on other sites

ok this works well for the first name, but not for anything else.. hmm

 

//Update order
echo'<form action="order.php" method="post">
         <input type="hidden" name="fname" value="'.$row['fname'].'" />
 <input type="hidden" name="lname" value="'.$row['lname'].'" />
 <input type="submit" value="Update-Order" />
     </form>';

 

and then:

<label>First
      <input type="text" name="fname" id="fname" value='<? echo "$fname" ?>' />
      </label>
        <label>Surname
          <input type="text" name="lname" id="lname" value='<? echo "$lname" ?>' />
        </label>

 

Anyone know why?

Link to comment
Share on other sites

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.