This question already has an answer here:
I am making a site that uses the file_get_contents to display a form. The form contains three fields and is as below. What I am trying to do is insert three values that I have generated from sql queries into this form.
Is there a way that I can send the values I have generated into the correct fields of the form?
Would I have to add some php to the top of the form that will allow it receive the values being passed from the file_get_contents function?
Calling the functions and form
//sql functions return values based on rowID
$updateTilte = $this->model->updateTitle();
$updatePrice = $this->model->updatePrice();
$updateDescription = $this->model->updateDescription();
$rightBox = file_get_contents( "templates/update_item_form.php" );
Update_item_form
<h2>Update Item!</h2>
<h4>Fill in the form to update an entry.</h4>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='updateItem' />
<p>
<label for="fTitle">Title</label> <input type="text"
id="fTitle" name="fTitle" placeholder="title"
maxlength="25" required />
</p>
<p>
<label for="fPrice">Price</label> <input type="text"
id="fPrice" name="fPrice" placeholder="price"
maxlength="25" required />
</p>
<p>
<label for="fDescription">Description</label> <input type="text"
id="fDescription" name="fDescription" placeholder="description"
maxlength="500" required />
</p>
<p>
<div class="form-group">
<div class="controls">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</p>
</fieldset>
</form>
</div>
No. That's not what file_get_contents does, and it's not what it's for either. What you are looking for is include
It opens the file in PHP mode, executing whatever code it finds. That means you can use the variables you declared above inside the file, and PHP will replace them with their value.
Snippets:
$updateTilte = $this->model->updateTitle();
include "templates/update_item_form.php";
Form:
<label for="fTitle">Title</label> <input type="text"
id="fTitle" name="fTitle" placeholder="title"
maxlength="25" required value="<?php echo $updateTilte; ?> />
Note: include
will immediately output the html in the file. Only include the file when you're ready to output everything.