dominicansell
dominicansell

Reputation: 83

Using a PHP variable as a form fields default value

I have a created an HTML form where users sign up and input there data into an SQL database. I have then retrieved that data in a webpage where they can view their profile. I have created a page where users can edit there profile by creating a form which updates the value in the SQL database for there user id.

I would like the form to use the current value of the SQL cell as the default for that user to make alterations easier. Example: currently user 7 has their city set as New York, when they visits the edit info page, the city field in the form already hase New York as the default value.

I have no problem getting the SQL info and assigning it to a variable, I just don't understand how to set it as the default value. I am aware of how you set default values for input fields though.

My code:

<?php
$id = $_SESSION["user_id"];    

// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";

// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);

// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];   
echo mysqli_error();    
}
}
?>  
<input type="text" name="aboutme" defualt="<?php echo $row['aboutme'] ?>" >  

Upvotes: 0

Views: 8632

Answers (4)

Sunday
Sunday

Reputation: 1

I hope you find my answer useful.

Why don't you try using it as a place holder? This will provide editable text.

<input type="text" name="aboutme" placeholder="<?php echo $row['aboutme'];" />

Upvotes: 0

dominicansell
dominicansell

Reputation: 83

Neither of the answers worked and upon further research and trial and error I created a solution.

I changed the value that was store in the array to just be a normal php variable:

$aboutme = $row['aboutme'];    

I then called that variable using the following code:

<input type="text" name="aboutme" value="<?php echo htmlspecialchars($aboutme); ?>" >

Thanks for your help.

Upvotes: 1

u_mulder
u_mulder

Reputation: 54841

There's no default value for html input. Input can has value, using attribute value:

<input type="text" name="some_name" value="Some value" /> 

In your case it's

<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> /> 

Input can also has placeholder - some value that is present in an input, but erased when user starts to edit input's content:

<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> placeholder="some value" /> 

Upvotes: 3

Nepal12
Nepal12

Reputation: 593

How about

<?php
$id = $_SESSION["user_id"];    

// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";

// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);

// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];   
?>
<input type="text" name="aboutme" value="<?php echo $row['aboutme'] ?>" > 
<?php
echo mysqli_error();    
}
}
?>  

And here is a good example http://www.w3schools.com/php/showphpfile.asp?filename=demo_db_select_pdo

Upvotes: 1

Related Questions