Undefined variable $submit - php

I'm getting an error which states:
Notice: Undefined variable: submit on line 4. I have no clue why since I have defined it with the submit button? Can someone please explain why? Don't really see the problem here.
<?php
if($submit)
{
$sql = "INSERT INTO personnel (first, last, username, department, email) VALUES ('$first','$last','$username','$department','$email')";
$add = mysql_query($sql);
echo "<div class='confirmation-box round'>Thank you! New user have been added</div>";
}
else
{
?>
<form method="post" action="useradd.php">
<fieldset>
<p>
<label for="simple-input">Firstname</label>
<input type="text" id="first" name="firstname" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Lastname</label>
<input type="text" id="last" name="lastname" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Username</label>
<input type="text" id="username" name="username" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Department</label>
<input type="text" id="department" name="department" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Email</label>
<input type="text" id="email" name="email" class="round default-width-input" />
</p>
<input type="Submit" name="submit" class="button round blue image-right ic-add text-upper" value="Add">
</fieldset>
</form>
<?php
}
?>
(Sorry for stupid question, kinda new at php)

This is a typical old school case of register_globals usage. It decides whether or not register the EGPCS (Environment, GET, POST, Cookie, Server) variables as global variables. Nobody does that anymore.
Change it to this:
if (!empty($_POST['submit'])) {

I have no clue why since I have defined it with the submit button
No you did not.
The first instance of $submit which is if($submit) means:
If $submit is TRUE
... $submit has never been defined before.

You need to check post with following code
if(isset($_POST['submit']))
{
//Here will be your code
}
try this
And good practice to give proper name to submit button. Ex. 'cmdSubmit'
Best luck

Its a warning not an error.
You can just declare the varible with empty value or some default value.

you can use
if(isset($_POST['submit']))
{
//code goes here
}

Related

Filling form data by using url extension; Why does this not work?

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>

Undefiend index

When I send my form to another page it says: Notice: Undefined index: gebruikersnaamRegistreren in on line 55
this for every name of the inputs
HTML Code
<form action="RegistrerenSucces.php" method="POST">
<fieldset id="inputs">
<input id="gebruikersnaam" type="text" name="gebruikersnaamRegistreren" placeholder="Gebruikersnaam " required />
<input id="paswoord" type="password" name="paswoordRegistreren" placeholder="Paswoord" required />
<input id="voornaam" type="text" name="voornaamRegistreren" placeholder="Voornaam" required />
<input id="achternaam" type="text" name="achternaamRegistreren" placeholder="Achternaam" required />
<input id="email" type="email" name="emailRegistreren" placeholder="E-mail adres" required />
<input id="straat" type="text" name="straatRegistreren" placeholder="Straatnaam en huisnummer" required />
<input id="postcode" type="text" name="postcodeRegistreren" placeholder="Postcode" required />
<input id="gemeente" type="text" name="gemeenteRegistreren" placeholder="Gemeente" required />
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Registreren" />
</fieldset>
</form>
PHP Code
<?php
$dbhost='localhost';
$dbuser='root';
$dbpassword='usbw';
$dbdatabase='computingstore';
$paswoordEncrypted = hash('sha512',$_POST['paswoordRegistreren']);
print_r($_POST);
/*Database verbinden*/
$link = mysqli_connect($dbhost , $dbuser ,$dbpassword , $dbdatabase);
$query = ("INSERT INTO gebruiker (gebruikersnaam, paswoord, voornaam, achternaam, e-mail, straatnaam, postcode, gemeente)
VALUES
('".$_POST['gebruikersnaamRegistreren']."',
'".$paswoordEncrypted."',
'".$_POST['voornaamRegistreren']."',
'".$_POST['achternaamRegistreren']."',
'".$_POST['emailRegistreren']."',
'".$_POST['straatnaamRegistreren']."',
".$_POST['postcodeRegistreren'].",
'".$_POST['gemeenteRegistreren']."')");
mysqli_query($link,$query);
/*Verbing afsluiten*/
mysqli_close($link);
?>
How can i fix this error i have been looking for it for 2 days now.
Try to use isset to determine if the index is defined and assign an empty value if it is not, and see if the problem resolves.
For example, use the following in the second page:
isset($_POST['gebruikersnaamRegistreren']) ? $_POST['gebruikersnaamRegistreren'] : "";
instead of
$_POST['gebruikersnaamRegistreren']
repeat this for all the fields for which you are having problem .
Everything seems to work perfectly. Try to use prepare for your queries though. Nevertheless I recommend you to check for any javascript issues.

How to store html form data in a cookie so it's still there after the form is submitted

I have made this html form, it submits all the information to a MySQL database. Pretty simple stuff.
How do I store the username in a cookie so that once it's entered? I would like to ensure that once the user enters their username once, it pre-populates the username field in the future.
<!-- Message board submissionform -->
<form id="frmMB" name="frmMB" action="insert.php" method="post" enctype="multipart/form-data">
<label class="name"><input name="name" placeholder="Enter your name" type="text" id="name" onFocus="if(this.value=='Enter your name'){this.value=''};" onBlur="if (this.value==''){this.value='Enter your name'};" value="Enter your name" size="80" maxlength="10" ></label>
<br />
<label class="message">
<input name="post" placeholder="Enter a message" type="text" id="post" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;" value="Enter a message" size="80" maxlength="140" data-maxsize="3">
</label>
<br />
<label>
<input name="Submit" class="large button" type="submit" value="submit">
</label>
I think something like this would just do this trick.
setcookie($_POST['name'], $value, time()+3600);
if(isset($_COOKIE['name']) && !empty($_COOKIE['name']))
{
// do some stuff here because the name is set in the cookie.
}
First you need to start a session -> session_start()
Once done so, you have created a public $_SESSION array and can add elements to it. Easiest way to do so is:
<?php
if(isset($_POST['username'])){
sesion_start();
$_SESSION['username'] = $_POST['username'];
}
...
Cheers!

unsubscribe html form using php and my sql

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?

How to access the form's 'name' variable from PHP

I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.

Categories