Jump to content

Display PHP Array Value Inside HTML Form Text Box


jponte

Recommended Posts

Hi,

I'm new to PHP and MySQL but so far so good trying to get done what I need to do. I seem to get stuck displaying an array value inside a form text box. This is what I have but it does not work

 

// Connects to your Database
mysql_connect("localhost", "root", "escrma") or die(mysql_error());
mysql_select_db("rma_portal") or die(mysql_error());

// Collects data from "users" table 
$data = mysql_query("SELECT * FROM cust_info WHERE Cust_id = 'TDBANK1'") 
or die(mysql_error());

// puts the "users" info into the $info array 
$info = mysql_fetch_array( $data );  

 

<input name="custID" type="text" size="45" VALUE="<? echo $info['Cust_ID']; ?>"

 

My echos and prints work fine. But I would like the data inside the text box to be modified if any.

 

echo $info['Password'];
print $info["Username"];

 

Any help will be appreciated

Try  this $info['cust_id']; instead  $info['Cust_ID']; and see if it works.

 

Also if inside box as current value u get smthg like "<? echo $info['Cust_ID']; ?>" then u should use longer php tags <?php ... ?>. Anyways to use short tag <? is bad practise since it depends of server configuration and traveling from one hoster to another u may have big issues, so dont be lazy always add those three chars "php"

Hi,

Thanks for the fast reply. I actually do see the code in the box. Howver when I add the PHP tags:

 

<td width="274"><?php <input name="ShipAttention" type="text" size="45" VALUE="<? echo $info['Cust_ID'] ?>"> ?> </td> 

 

I get the error:

 

Parse error: syntax error, unexpected '<' in C:\Documents and Settings\jack.ponte\Desktop\Website\form1.php on line 258.

 

Thanks for your help

No the correct way is

<td width="274"><input name="ShipAttention" type="text" size="45" VALUE="<?php echo $info['Cust_ID'] ?>"></td>

 

NOTE: Tags such as <? ?> and <?= ?> may not always work as a setting called short_open_tag may be disabled. Always use the full PHP tags, eg <?php ?>

 

short_open_tags = On.

 

also <? echo $var ?> and <?=$var?> not always works with form correctly.

example:

$var = "Sander van Doorn";

u can experience problem in INPUT will appear only 'Sander'

in this case try using <?=nl2br($var)?>, works well.

 

hope was helpful.

short_open_tags = On.

 

also <? echo $var ?> and <?=$var?> not always works with form correctly.

example:

$var = "Sander van Doorn";

u can experience problem in INPUT will appear only 'Sander'

That is completely wrong. The reason why 'Sander' will only appear in the input box is probably because you're not wrapping your HTML attribute values in quotes.

 

Wrong

$somevar = "Sander van Doorn";
echo "<input type=text name=somename value=$somevar />";

The above will result in just 'Sande' being showing in the text box. It will treat 'van' and 'Doorn' as HTML attributes. All attribute values SHOULD be wrapped in quotes to prevent problems like this.

 

The correct way is

$somevar = "Sander van Doorn";
echo "<input type=\"text\" name=\"somename\" value=\"$somevar\" />";

//OR
echo '<input type="text" name="somename" value="'.$somevar.'" />';

 

in this case try using <?=nl2br($var)?>, works well.

I doubt it. Do you even know what nl2br is used for.

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.