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';
}
?>

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; '>

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?

Try this:

 

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

 

<input type="reset" value="Reset Fields" onclick="resetFields()" />

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.

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>

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.