This question already has an answer here:
PHP Using File_get_Contents() to pre populate a form [closed]
(1 answer)
Closed 7 years ago.
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>
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.
Related
This question already has answers here:
Trigger standard HTML validation (form) without using submit button? [duplicate]
(13 answers)
Closed 7 months ago.
I'm new to coding in HTML. After hours of searching the internet for a way to do this, I failed and so I'm here. I want to redirect user to another page ONLY AFER USER FILLED ALL INPUTS IN THE FORM
but here if user will click submit it will redirect to next page.
<form action="home.php" method="post">
<div>
<input type=text name="username">
<label>Email or phone number</label>
</div>
<div>
<input type=text name="username">
<label>Phone</label>
</div>
<button type=submit name="submit">Sign In</button>
Use the required attribute. Example below:
<form action="home.php" method="post">
<div>
<input type=text name="username" required>
<label>Email or phone number</label>
</div>
<div>
<input type=text name="username" required>
<label>Phone</label>
</div>
<button type=submit name="submit">Sign In</button>
You can combine this attribute with other attributes, like pattern, to define a requirement in a more specific input.
At the same time, it is still necessary to check user input on the server.
You should use radio box. For example your html code:
<input type="radio" name="contact" id="contact_email" value="email" />
<label for="contact_email">Email</label>
and now, you can check if it's "checked" with php code below:
if(isset($_POST['contact'])) {
// code to redirect, for example header...
}
I am trying to save a Users data from an input field so that it can be displayed later in their profile of a webpage, for example the user inputs data of a cinema(name, address) and can see it later under Saved Restaurants and call up the previously saved information. Can the PHP and HTML code be written together in one .PHP file?
So far I have this:
<html lang>
<head>
<link rel="stylesheet" href="css/addOrEditCinemaPage.css">
</head>
<?php include "php/head.php" ?>
<?php include "php/navigation.php" ?>
<body>
<div class="myForm">
<form>
<h2>Add or Edit a Cinema</h2>
<label for="name"><b>Name of Cinema</b></label>
<input type="text" placeholder="Enter name">
<label for="str"><b>Street</b></label>
<input type="text" placeholder="Enter street">
<label for="nr"><b>Nr.</b></label>
<input type="number" placeholder="Enter Nr.">
<label for="plz"><b>Post Code</b></label>
<input type="number" placeholder="Enter Post Code"><br><br>
<label for="ct"><b>City</b></label>
<input type="text" placeholder="Enter City">
<label for="sta"><b>State</b></label>
<input type="text" placeholder="Enter State">
<label for="descr"><b>Description</b></label><br>
<textarea placeholder="Enter Discription"></textarea>
<div class="imagebutton">
Add Image
</div>
<button type="submit">Save</button>
</form>
</div>
<?php include "php/footer.php" ?>
</body>
</html>```
Can the PHP to save and display to input infomration also be written here?
Yes, you can by setting the 'action' attribute of the form to the same file, and by setting the 'method' attribute to POST.
Instead of using
<form>
use
<form action="<?PHP echo $_SERVER['php_self'];?>" method="POST">
Then, set the 'name' attribute of each input.
For example, Instead of using
<input type="text" placeholder="Enter name">
use
<input type="text" name="name" placeholder="Enter name">
You'll also have to set the 'name' attribute of the submit button to 'submit':
<button type="submit" name="submit">Save</button>
Once you've done that, the PHP code to access the form data would be:
if (isset($_POST['submit'])) {
echo $_POST['name'];
}
send your data with html to another php page
you must use get or post for sending form data to php page
like below
This question already has answers here:
PHP not rendering in HTML
(1 answer)
PHP script not working in HTML file
(6 answers)
PHP inside HTML doesn't work
(5 answers)
Closed 1 year ago.
My code is
<form action = "insert.php" method = "POST">
<div class="title-block">
<input type="text" name="rater_full_name" />
</div>
<div class="title-block">
<input type="text" name="rater_company_name" required />
</div>
<div class="title-block">
<input type="text" name="rater_ref" />
</div>
<div class="title-para">
<textarea rows="5" name="rater_suggestion"></textarea>
</div <div align="left"><div class="btn-block">
<input type="hidden" name="PID" value="<?php echo $PID; ?>"> <button type="submit" name="submit" value="submit">Submit Feedback</button>
</div>
</form>
It's not storing the variable $PID value, it is storing complete code inside quotations("") Please suggest best way to do so..
Thanks in advance.
Save the file as .php rather than .html
I have been trying to pre-fill the subject input with information generated in another page, but have been having difficulties with it despite reading a lot of resources about it and seeing examples. I have tried a variety of links, including my most recent attempt with http://www.myurl.com/folder/index.php/contactform?subject=test, but even that doesn't work. Anybody have any advice? Also, if you want to test it out before answering, the page experiencing the problem is the contact page of this website. I've removed information from below to make it more general. Thanks in advance for any and all of the help.
<form id="contactform" method="post">
<input name="recipient" type="hidden" value="myemail" />
<input name="subject" type="hidden" value="Contacter" />
<p id="contactname">
<label>Name:</label>
<input name="name" type="text" />
</p>
<p id="contactemail">
<label>Email:</label>
<input name="email" type="text" />
</p>
<p id="title">
<label>Subject:</label>
<input name="title" type="text" />
</p>
<p id="contactmessage">
<label>Message:</label>
<textarea name="message"></textarea>
</p>
<p id="submit">
<input type="button" value="Send" />
</p>
<input name="redirect" type="hidden" value="myredirectpage" />
</form>
Lets say your page URL is some thing like below
http://www.example.net/index.php?var1=Something&var2=10&var3=ok
You can use $_GET to get the values of var1, var2,and var3 from the above url
In index.php use the below code to fetch url data
echo $_GET['var1'] // Something
echo $_GET['var2'] // 10
echo $_GET['var3'] // ok
Go through this link http://php.net/manual/en/reserved.variables.get.php
With php you can do this with a session.
On the other page(not the form) you can do $_SESSION['subject'] = 'your subject';
On the form page you can acces this cookie ( make sure you have started the session on top of the page with session_start():
<p id="title">
<label>Subject:</label>
<input name="title" type="text" value="<?= $_SESSION['subject'] ?>"/>
</p>
I hope someone can help me. I'm a little at loss on how I can achive this: I'm trying to pass infos generated in my php to another form made in Javascript. For example, if I put my first name in the input field and click submit, then it would go to another page with the actual form and have the first name already filled there.
One thing that I did notice was that the javascript form isn't within the <form> tag, it's in a bunch of tables with some input fields. I can post what it looks like in pastebin if this can help in understanding what I mean.
Also with this script im unable to edit it, I did not make it, it is one of those script you just place on your site thats auto generated.
My form looks like this:
<form action="auto-form/index.php" method="post" name="openleads" onsubmit="return checkForm(this);">
<label for="first">First Name <span class="required">*</span></label>
<input id="first_name" type="text" name="first" applicationforms="true" /><br>
<label class="error" for="name" id="first_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Zip <span class="required">*</span></span>
<input id="zip" type="text" name="zip" applicationforms="true" /><br>
<label class="error" for="name" id="zip_error" style="color:#F00; font-size:11px;">This field is required.</label>
<label for="last">Last Name <span class="required">*</span></label>
<input id="last_name" type="text" name="last" applicationforms="true" /><br>
<label class="error" for="name" id="last_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Email <span class="required">*</span></span>
<input id="email" type="text" name="email" applicationforms="true" /><br>
<label class="error" for="name" id="email_error" style="color:#F00; font-size:11px;">This field is required.</label>
<input class="button" type="submit" name="send" value="Send" />
</form>
Any help is appreciated; like I said, I'm a bit at loss on what to do with this one.
php-form.php
<form action="javascript-form.php" method="post">
<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
javascript-form.php
<form action="" method="">
<input type="text" name="name" value="<?= (isset($_POST['name'])?htmlentities($_POST['name'],ENT_QUOTES):''); ?>" />
<input type="submit" value="Submit" />
</form>
Use PHP to output the POSTed values in to the value attribute of the form fields. You can also use GET variables and use javascript to parse the window.location and scrape those form values.
this seemed to get this done
<script type="text/javascript" src="http://jquery.offput.ca/js/jquery.timers.js"></script>
<script>
$(document).everyTime(1000,function(i){
if($('#ui-datepicker-div').length>0)
{
$('#first_name').val('<?php echo $_POST['first_name']; ?>');
$('#last_name').val('<?php echo $_POST['last']; ?>');
$('#zip').val('<?php echo $_POST['zip']; ?>');
$('#email').val('<?php echo $_POST['email']; ?>');
}
})
$('#first_name').val('test');
</script>