Form information not being 'seen' by MySQL - php

I've made a new log in / register template which uses CSS3 and HTML, yet I had a working form, but very basic, before this. So I decided to make another 'form' which should look like this:
http://www.script-tutorials.com/css 3-modal-popups/
Now, I use this form to handle my registration:
<!-- Popup Form #2 -->
<div class="popup">
<h2>Sign Up</h2>
<p>
Please enter your details here.
</p>
<div>
<form action="register.php" action="post">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Max 32 characters" name="username" />
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" placeholder="Password" name="password" />
</div>
<div>
<label for="lastname">Lastname</label>
<input type="text" id="lastname" placeholder="Lastname" name="lastname" />
</div>
<div>
<label for="email">E-mail</label>
<input type="email" id="email" placeholder="example#hotmail.com" name="email" />
</div>
<input type="submit" value="Join the Community" /> or
</form>
Yet when I test this form, it will say the data is stored (My register.php file comes with a if-statement), yet it displays nothing. (No username, (password still gets hashed / stored, though I'm not sure if it's really handling it), last name and e-mail.
I've checked and double checked the 'name' attributes' value in the register.php file. And those are correct.

Pretty sure you are accessing your form values using $_POST. The problem is that in your form tag you are setting action twice, one of which is correct i.e. action="register.php" and the other action="post" is incorrect.
Try:
<form action="register.php" method="post">

and remember to enclose variables in quotes, for example
$foo = $_POST['foo'];
$bar = $_POST['bar'];
$sql_query = "INSERT INTO my_table (foo, bar) VALUES ('$foo', '$bar');";

Related

Saving and displaying user input using HTML and PHP

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

HTML and PHP - How to seperate 2 forms on a single page?

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>

inserting a premade email contact form into website

I have very little knowledge of php so I went ahead and used one from css-tricks email forms he set up for free use:http://css-tricks.com/nice-and-simple-contact-form/.
However I am not sure how to put it into the website without breaking the php code. I assume I can't simply just take the code that I want, which is
<div id="contact-area">
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
<div style="clear: both;"></div>
<p>Check out a version of this with SPAM protection.</p>
</div>
and put it into my html page. I tried using an iframe to link the html page into my html page for my website and it worked, but is this method ok?
Yes, you can just place that coding in your HTML page as long as all the webpage and css file's are linked correctly.(e.g. In the same folder as this page with the coding you have now.)
Using an i-frame for this is not a good idea as it can really mess up your pages and submitting your form.

Ajax submit multiple forms using a single forms' values

So lets say I have some forms like this:
Form A:
<form name="formA">
<input type="text" name="username">
<input type="text" name="password">
</form>
Form B:
<div style="display:none;">
<form name="formB">
<input type="text" name="username">
<input type="text" name="password">
</form>
</div>
Form C:
<div style="display:none;">
<form name="formC">
<input type="text" name="username">
<input type="text" name="password">
</form>
</div>
Forms B and C are hidden, I have them there because after a user enters their username and password in form A, I need to also use that username and password information they submitted to be used in forms B and C, and then submit all forms at once. This is so I can submit a login to multiple parts of my website using just 1 login form. How exactly should I go about doing this?
-Thanks!
If you don't need https for login function, you can try this using jQuery and ajaxForm plugin:
<form name="formA" action="actionA">
<input id="usernameA" type="text" name="username">
<input id="passwordA" type="password" name="password">
</form>
<div style="display:none;">
<form name="formB" action="actionB">
<input id="usernameB" type="text" name="username">
<input id="passwordB" type="password" name="password">
</form>
</div>
<div style="display:none;">
<form name="formC" action="actionC">
<input id="usernameC" type="text" name="username">
<input id="passwordC" type="password" name="password">
</form>
</div>
$('#formA').submit(function() {
var username = $('#usernameA').val();
var password = $('#passwordA').val();
$('#usernameB').val(username);
$('#passwordB').val(password);
$('#usernameC').val(username);
$('#passwordC').val(password);
$('#formB').ajaxForm(function() {
alert("You have been logged in at B");
});
$('#formC').ajaxForm(function() {
alert("You have been logged in at C");
});
});
When you send an ajax request to the server for login at B and C, if your server send back a cookie on successful login, I think you're good to go :P
You can use multiple values for each variable name. For example:
<form name="formABC">
<div id="formA">
<input type="text" name="username[]">
<input type="text" name="password[]">
</div>
<div id="formB" style="display:none;">
<input type="text" name="username[]">
<input type="text" name="password[]">
</div>
<div id="formC" style="display:none;">
<input type="text" name="username[]">
<input type="text" name="password[]">
</div>
</form>
1) You should be using type="password" for password input fields.
2) You cannot submit more than one form at a time. Only the data from the form that is being submitted will be sent. From what I understand that you're trying to do, you want one button to open three different pages to three different login forms to store three different sessions. There are better ways to make your sessions (or cookies) work across multiple sections of your website, such as attaching the session ID to the URL so you always have it for every page load.
3) A form needs to have an action defined.

Passing php form info to javascript form

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>

Categories