I am making a PHP project and am trying to change an input boxes value according to a PHP variable, how would I do this? So far, when I try to change the input boxes value it just changes the value to the name of the variable.
Here's the html for that section:
echo '
<html>
<title>Ramen</title>
<body>
<h1>
<p>Welcome to NoteBook!</p>
<form method="post">
<p>File Name:<input type="text" name="fname"></p>
<p>Type Here:<input type="text" name="content" id="TextBox" value="<?php echo $value; ?>"><p>
<p>Save:<input type="submit" value="Save"></p>
</form>
<form method="post">
<p>File to Open: <input type="text" name="filename"></p>
<input type="submit" value="Open">
</form>
</h1>
</body>
<style>
body{
background-color:#66b3ff;
}
p{
margin-left:30px;
}
h1{
background-color:white;
height:600px;
border-radius:1px;
font-size:18px;
font:italic;
}
#TextBox{
height:500;
width:700;
}
{
</style>
</html>```
CodePudding user response:
$value
doesn't appear to be being set anywhere.
I'm also a little confused as to why you have two <form>
s.
As a starter, you need to parse the values coming in from the form, for example:
<?php
if (isset($_POST["filename"]) { // The $_POST variables are set using the name element of the <input> tag
$value = $_POST["filename"]
}
?>
You would likely want to do some more validation on the content of $_POST["value"]
before accepting it.
Perhaps take a look at this example to get you started.
CodePudding user response:
I leave you a possible example with explanation.
Besides, I have adapted the HTML structure a bit, since it has serious formatting problems. I advise you to read the manual on how to use the labels.
I added the name attribute to the submit, in order to know if the form is defined:
<input type="submit" name="save_data" value="Save">
Example:
<?php
// reset ¡important! (avoid warning <b>Warning</b>: Undefined variable etc..)
$file_name = $content = $txt_content = '';
// Recomended to check if the form is define
if (isset($_POST['save_data'])) {
// Get the inputs data
$file_name = $_POST['fname'] ?? '';
$content = $_POST['content'] ?? '';
// You can use textarea
$txt_content = $_POST['txt_content'] ?? '';
// Data is true
if ($file_name && $content && $txt_content) :
// Do somenting (ex: save it in DB)
//
//
$success = "Save correctly: $file_name";
// Data is empty (optional)
else :
$error = 'Te fields are empty.';
endif;
}
?>
<html lang="en-EN">
<head>
<title>Ramen</title>
<style>
body{
background-color:#66b3ff;
}
p {
margin-left:30px;
}
.wrapper {
background-color:white;
height:600px;
border-radius:1px;
font-size: 1rem;
font:italic;
padding: 1rem;
}
textarea {
height:200px;
width:400px;
}
.error {
color: red;
}
.success {
color: green;
}
</style>
</head>
<body>
<div >
<h1>Welcome to NoteBook!</h1>
<form method="post">
<lable for="fname">File Name:</lable>
<input type="text" name="fname" id="fname" value="<?php echo $file_name; ?>"></p>
<label for="content">Type Here:</label>
<input type="text" name="content" id="content" value="<?php echo $content; ?>">
<!-- for the content you could use the textarea tag -->
<textarea name="txt_content"><?php echo $txt_content; ?></textarea>
<input type="submit" name="save_data" value="Save">
<?php if (isset($error)) echo '<p >' . $error . '</p>'; ?>
<?php if (isset($success)) echo '<p >' . $success . '</p>'; ?>
</form>
</div>
</body>
</html>