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
Related
I'm pretty new to HTML and currently I'm making an assignment where I have to make a HTML Form for data input and write the input to a file using PHP.
The writing to file works, but I getting the following comment on my code:
You have tested whether something has been posted, but what happens to your code if there are 2 forms on your page?
I kinda know what is being meant with this, but I am not that fluent to know how to solve this issue or where this comes from in my code.. Does this has to do with the action of the form set to $_SERVER["PHP_SELF"], the name of the submit button set wrong or anything else?
I've tried looking online for HTML forms and how to have 2 on the same page.. but I could not find anything really helpfull. If anyone can help or maybe point me to some info that explains this in detail (and with examples preferably) that would be great!
Here is just my HTML form as I have it with the PHP part checking for the submit. Rest of the code I left out as it is not relevant..
<html>
<head>
<title>Save and show data</title>
</head>
<body>
<h2>Fill in the form below</h2>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<label for='name'>Naam:</label><br>
<input type="text" id="name" name="name" placeholder = "John Doe" required><br>
<label for='address'>Adres:</label><br>
<input type="text" id="address" name="address" placeholder = "Sunset lane 10" required><br>
<label for='zip'>Postcode:</label><br>
<input type="text" id="zip" name="zip" placeholder = "15922" required><br>
<label for='residence'>Woonplaats:</label><br>
<input type="text" id="woonplaats" name="woonplaats" placeholder = "Somewhere" required><br>
<label for='phone'>Telefoonnummer:</label><br>
<input type="tel" id="phone" name="phone" placeholder = "0678945124" required><br>
<label for='email'>E-mail:</label><br>
<input type="email" id="email" name="email" placeholder = "johndoe#email.com" required><br><br>
<input type="submit" name="submit_data" value="Save"><br>
</form>
<?php
if(isset($_POST["submit_data"]))
{
// magic stuff happens here
}
Do you mean something like this? Each submit button has its own validation.
<?php
if (isset($_POST['submit_data'])) {
//first form
}
if (isset($_POST['edit_data'])) {
//second form
}
?>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<label for='name'>Naam:</label><br>
<input type="text" id="name" name="name" placeholder = "John Doe" required><br>
<input type="submit" name="submit_data" value="Save"><br>
</form>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<label for='name'>Naam:</label><br>
<input type="text" id="name" name="name" placeholder = "John Doe" required><br>
<input type="submit" name="edit_data" value="Edit"><br>
</form>
this is a page that displays a list of creatives, and the form offers search functionality to search by job title:
if(isset($_POST['creatives-submit'])){
$job = $_POST['job-title'];
$data = \Db::Common($fms5->DBH)->getWhere("creatives", "creatives_active", "Yes"," AND creatives_job LIKE '%".$job."%'")->orderBy('creatives_name', 'asc');
}
<form method="post" name="creative-search">
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
is there anything that's obviously wrong my my code?
try changing if(isset($_POST['creatives-submit'])) to if(isset($_POST['job-title']) && !empty($_POST["job-title"])) as the form is posting the job-title value and this is the value you actually care about. (Since creatives-submit will always = Submit)
also change
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
to <input class="form-control" type="text" name="job-title" id="job-title" placeholder="Search by job title" required/>
this means the form can't be submitted unless the job-title field has a value and had the correct type of text
Below is a modification of your code that just returns what the user searched for (Since I don't have it connected to a database)
<?php
if(isset($_POST['job-title']) && !empty($_POST["job-title"])){
$job = $_POST['job-title'];
?>
<p>You Searched For <?php echo $job;?></p>
<?php
}
?>
And the form
<!-- Search Form -->
<form method="post" name="creative-search">
<input class="form-control" required="required" type="text" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.
Here is the code on the index.html page where the form is
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
Here is the code for send.php
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database .
I found this tutorial which was kind of helpful -
http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
and this is the form on my unsubscribe.html page.
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank.
So I am guessing I can't have two "post" method maybe??
If someone could guide me in the right direction that would be much appreciate. Thanks.
I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.
Example: in your subscribe.php
you should have :
<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />
in send.php
you must do:
if(!isset($_POST['subscribe'])
{
header('location: subscribe.html');
}
You must use isset for your pages.
If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.
Your delete.php script should be:
<?php
require ('connection.php'); // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";}
?>
your delete.php seems OK to me.
Can add the following to Line 2
echo "";
print_r($_POST);
and post array in comments?
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>
HTML code :
I echo this content in php file, In result i get only the 'Contact Us' Remaining part does not display the html form
<div > Contact Us
<form name="profile" action="" method="post">
Name : <input type="text" name="fullname" id="fullname" />
Email : <input type="text" name="email" id="email" />
Mobile : <input type="text" name="mobile" id="mobile" />
</form>
</div>
Jquery code response :
$.post("contact_us.php",{ user_id:$('#user_id').val(),typ:'contact_us' },
function(data) {
$('#contact_us').html(data);
});
In contact_us id does not write the contact us form, Please anyone help me
Have you tried using Firebug?
http://getfirebug.com/
That will let you see what the script is returning.
UPDATE:
You are referencing a container with ID = "contact_us"
$('#contact_us').html(data);
You need to set the ID of the div you want to replace the content in.
<div id="contact_us"></div>
Your HTML isn't valid. You need to close the div tag
<div >
Contact Us
<form name="profile" action="" method="post">
Name : <input type="text" name="fullname" id="fullname" />
</form>
</div>