Log in

View Full Version : How to insert value to textfield in PHP?



devil_vin
09-08-2007, 03:56 PM
For the code below,how can I insert $value into the textfiled respectively?And did there any code like setEditable=false in Java to disable editing of particular textfield?Thanks...




<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);
if ($numrow != 0)
{
// fetch each record in result set
for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
{
// build table to display result
print ("<h2><center>Member Profile</center></h2>");
print ("<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
print ("<tr>");
print ("<th>Full Name</th>");
print ("<th>Telephone</th>");
print ("<th>Address</th>");
print ("</tr>");
print ("<tr>");

foreach ($row as $key => $value)
print ("<td><input type ='text'size = '30'></td>");

print ("</tr>");
print ("</table>");
}
}
}
?>

thetestingsite
09-08-2007, 04:03 PM
Try this for the inserting of the field.



<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);
if ($numrow != 0)
{
// fetch each record in result set
for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
{
// build table to display result
print ("<h2><center>Member Profile</center></h2>");
print ("<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
print ("<tr>");
print ("<th>Full Name</th>");
print ("<th>Telephone</th>");
print ("<th>Address</th>");
print ("</tr>");
print ("<tr>");

foreach ($row as $key => $value)
print ("<td><input type ='text' value='$value' size = '30'></td>");

print ("</tr>");
print ("</table>");
}
}
}
?>


As for the editing of only certain fields, you can append "readonly" to the field that you don't want editable. This would look something like the following:



<input type="text" name="blah" value="blah blah" readonly>


Hope this helps.

devil_vin
09-08-2007, 04:13 PM
Try this for the inserting of the field.



<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);
if ($numrow != 0)
{
// fetch each record in result set
for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
{
// build table to display result
print ("<h2><center>Member Profile</center></h2>");
print ("<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
print ("<tr>");
print ("<th>Full Name</th>");
print ("<th>Telephone</th>");
print ("<th>Address</th>");
print ("</tr>");
print ("<tr>");

foreach ($row as $key => $value)
print ("<td><input type ='text' value='$value' size = '30'></td>");

print ("</tr>");
print ("</table>");
}
}
}
?>


As for the editing of only certain fields, you can append "readonly" to the field that you don't want editable. This would look something like the following:



<input type="text" name="blah" value="blah blah" readonly>


Hope this helps.


oh..I don't realize existence of this feature,thanks...

devil_vin
09-08-2007, 04:32 PM
If I change address field to textarea then how each $value being fixed to its location?The following code seems lost the textfield and textarea...thanks..



foreach ($row as $key => $value)
if($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value'readonly></td>");
else if($row == 2)
print("<td><textarea rows = '3' value = '$value'></textarea></td>");

thetestingsite
09-08-2007, 04:53 PM
For textareas, you place the default value (what you want displayed) in between the <textare> tags like so:



foreach ($row as $key => $value)
if($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
else if($row == 2)
print("<td><textarea rows = '3'>$value</textarea></td>");


Hope this helps.

devil_vin
09-09-2007, 07:39 AM
For textareas, you place the default value (what you want displayed) in between the <textare> tags like so:



foreach ($row as $key => $value)
if($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
else if($row == 2)
print("<td><textarea rows = '3'>$value</textarea></td>");


Hope this helps.


Unfortunately,y textarea and textfield still gone,this is the table structure:



print ("<h2><center>Member Profile</center></h2>");
print ("<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
print ("<tr>");
print ("<th>Full Name</th>");
print ("<th>Telephone</th>");
print ("<th>Address</th>");
print ("</tr>");
print ("<tr>");

foreach ($row as $key => $value)
if($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
else if($row == 2)
print("<td><textarea rows = '3'>$value</textarea></td>");

print ("</tr>");
print ("</table>");

print ("<table>");
print ("<tr>");
print ("<td>&nbsp;</td>");
print ("<td width='4%'>&nbsp;</td>");
print ("<td></tr>");

print ("<tr>");
print ("<td>&nbsp;</td>");
print ("<td width='4%'>&nbsp;</td>");
print ("<td><input type='submit' name='submit' value='Upgrade'></td>");
print ("</table>");

alexjewell
09-09-2007, 11:53 AM
I would break out of PHP, first of all, for just HTML:



<?php ?>
<h2><center>Member Profile</center></h2>
<!-- rest of code before foreach statement
<?php

foreach ($row as $key => $value)
if($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
else if($row == 2)
print("<td><textarea rows = '3'>$value</textarea></td>");
?>

</tr>
</td>
<!-- rest of html here -->


Now, for the textarea, it's better to write the readonly like, readonly="readonly". What you have now will work, though you might want to replace it with something more valid.

devil_vin
09-09-2007, 02:18 PM
I think I have confusing with html and php clauses...:confused:
What went wrong in my code?Thanks...




<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
?>
// build table to display result
<html>
<title></title>
<head>
<body>
<h2><center>Member Profile</center></h2>
<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
<tr>
<th>Full Name</th>
<th>Telephone</th>
<th>Address</th>
</tr>
<tr>

<?

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);

if($numrow != 0)
{// fetch each record in result set
for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
{
foreach ($row as $key => $value)
if ($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
else
if ($row == 2)
print ("<td><textarea rows = '3'>$value</textarea></td>");
}
}
}
?>

</tr>
</table>
<table>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td></tr>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td><input type='submit' name='submit' value='Upgrade'></td>
</table>
</body>
</head>
</html>





I would break out of PHP, first of all, for just HTML:



<?php ?>
<h2><center>Member Profile</center></h2>
<!-- rest of code before foreach statement
<?php

foreach ($row as $key => $value)
if($row == 0 && $row == 1)
print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
else if($row == 2)
print("<td><textarea rows = '3'>$value</textarea></td>");
?>

</tr>
</td>
<!-- rest of html here -->


Now, for the textarea, it's better to write the readonly like, readonly="readonly". What you have now will work, though you might want to replace it with something more valid.

thetestingsite
09-09-2007, 02:55 PM
Try this instead (my changes are highlighted):



<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
?>
// build table to display result
<html>
<title></title>
<head>
<body>
<h2><center>Member Profile</center></h2>
<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
<tr>
<th>Full Name</th>
<th>Telephone</th>
<th>Address</th>
</tr>
<tr>

<?

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);

if($numrow != 0)
{// fetch each record in result set
for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
{
foreach ($row as $key => $value)
if ($key == 0 && $key == 1)
?>
<td><input type="text" size="30" value="<?php echo $value;?>" readonly="readonly"></td>
<?php
else
if ($row == 2)
?>
<td><textarea rows="3"> <?php echo $value; ?> </textarea></td>
<?php
}
}
}
?>

</tr>
</table>
<table>
<tr>
<td>&nbsp;</td>
<td width='4&#37;'>&nbsp;</td>
<td></tr>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td><input type='submit' name='submit' value='Upgrade'></td>
</table>
</body>
</head>
</html>


Hope this helps.

devil_vin
09-09-2007, 04:01 PM
Thanks for your help.However it showed syntax error of unexpected T_ELSE in line of else statement.



Try this instead (my changes are highlighted):



<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
?>
// build table to display result
<html>
<title></title>
<head>
<body>
<h2><center>Member Profile</center></h2>
<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
<tr>
<th>Full Name</th>
<th>Telephone</th>
<th>Address</th>
</tr>
<tr>

<?

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);

if($numrow != 0)
{// fetch each record in result set
for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
{
foreach ($row as $key => $value)
if ($key == 0 && $key == 1)
?>
<td><input type="text" size="30" value="<?php echo $value;?>" readonly="readonly"></td>
<?php
else
if ($row == 2)
?>
<td><textarea rows="3"> <?php echo $value; ?> </textarea></td>
<?php
}
}
}
?>

</tr>
</table>
<table>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td></tr>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td><input type='submit' name='submit' value='Upgrade'></td>
</table>
</body>
</head>
</html>


Hope this helps.

thetestingsite
09-09-2007, 04:19 PM
Oops, sorry. Forgot some brackets there.



<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
?>
// build table to display result
<html>
<title></title>
<head>
<body>
<h2><center>Member Profile</center></h2>
<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
<tr>
<th>Full Name</th>
<th>Telephone</th>
<th>Address</th>
</tr>
<tr>

<?

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);

if($numrow != 0)
{// fetch each record in result set
for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
{
foreach ($row as $key => $value) {
if ($key == 0 && $key == 1) {
?>
<td><input type="text" size="30" value="<?php echo $value;?>" readonly="readonly"></td>
<?php
}
else {
if ($row == 2) {
?>
<td><textarea rows="3"> <?php echo $value; ?> </textarea></td>
<?php
}
}
}
}
}
}
?>

</tr>
</table>
<table>
<tr>
<td>&nbsp;</td>
<td width='4&#37;'>&nbsp;</td>
<td></tr>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td><input type='submit' name='submit' value='Upgrade'></td>
</table>
</body>
</head>
</html>


That should work, but not tested.
Hope this helps.

devil_vin
09-09-2007, 04:39 PM
So weird...the textfield and textarea still missing....:confused: :confused:


Oops, sorry. Forgot some brackets there.



<?php
include ('dbconn.cfg');// database configuration file
$connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
?>
// build table to display result
<html>
<title></title>
<head>
<body>
<h2><center>Member Profile</center></h2>
<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
<tr>
<th>Full Name</th>
<th>Telephone</th>
<th>Address</th>
</tr>
<tr>

<?

if (isset($_SESSION['gmembername']))
{
$tbl_name = "member";
$sql = "SELECT name,telephone,address FROM $tbl_name";

$result = @mysql_query($sql, $connection) or die("Cannot execute query.");
$numrow = mysql_num_rows($result);

if($numrow != 0)
{// fetch each record in result set
for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
{
foreach ($row as $key => $value) {
if ($key == 0 && $key == 1) {
?>
<td><input type="text" size="30" value="<?php echo $value;?>" readonly="readonly"></td>
<?php
}
else {
if ($row == 2) {
?>
<td><textarea rows="3"> <?php echo $value; ?> </textarea></td>
<?php
}
}
}
}
}
}
?>

</tr>
</table>
<table>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td></tr>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td><input type='submit' name='submit' value='Upgrade'></td>
</table>
</body>
</head>
</html>


That should work, but not tested.
Hope this helps.

devil_vin
09-10-2007, 04:31 AM
Anyone know that what went wrong in my script?If I only use three textfields then fine,however I can't make any further specifications, so I mix with textarea by specifying it in foreach loop.But all textfields and textarea gone missing...:confused: Thanks....:)

DevilsPride
02-01-2008, 12:31 AM
Text fields can't have spaces in them. Text areas can. That's the problem. use string replace or remove them to get the value of a text field to write all then info from the $_REQUEST object.

<input type="text" name="name" size="30" value=<?=(isset($_POST['name']))?str_replace(' ',"&nbsp;",$_POST["name"]):''?>>