PHP Unidentified index, separate php and html files - php

i know this topic has been touched on so much, but a solution is still evading me. I keep getting an unidentified index error in this php file:
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$birthDate = isset($_POST["birthDate"]) ? $_POST['birthDate'] : '';
$gender = isset($_POST['gender']) ? $_POST['email'] : '';
$comments = isset($_POST['comments']) ? $_POST['comments'] : '';
?>
Thanks for submitting your application!<br>
The following is the information we received:<br>
Name: <?php echo $_POST['name'] ?><br>
Telephone: <?php echo $_POST['telephone']?><br>
E-Mail: <?php echo $_POST['email']?><br>
Birthday: <?php echo $_POST['birthDate '] ?><br>
Gender: <?php echo $_POST['gender'] ?><br>
When you first wanted to be a zookeeper: <?php echo $_POST['comments ']
?>
</body>
</html>
here's the html form that this php file is getting it's values from:
<form id="zooKeeperForm" action="zoo.php" method="GET" onsubmit="return validateForm()">
<p><i>Please complete the form. Mandatory fields are marked with a </i><em>*</em></p>
<fieldset>
<legend>Contact Details</legend>
<label for="name">Name <em>*</em></label>
<input id="name" placeholder="Jane Smith" autofocus required><br>
<label for="telephone">Telephone</label>
<input id="telephone" placeholder="(xxx) xxx-xxxx"><br>
<label for="email">Email <em>*</em></label>
<input id="email" type="email" required><br>
</fieldset>
<fieldset>
<legend>Personal Information</legend>
<label for="birthDate">Birth Date<em>*</em></label>
<input id="birthDate" type="date" required><br>
<label for="age">Age<em>*</em></label>
<input id="age" type="number" min="0" max="120" step="0.1" required><br>
<label for="gender">Gender</label>
<select id="gender">
<option value="female">Female</option>
<option value="male">Male</option>
</select><br>
<label for="comments">When did you first know you wanted to be a zoo-keeper?<em>*</em></label>
<textarea id="comments" oninput="validateComments(this)" required></textarea>
</fieldset>
<p><input type="submit" value="Submit Application"></p>
</form>
what am I doing wrong here?? I feel like an idiot. I keep going from getting the unidentified index error to where it prints out the format I want but it's just blank for all the values that should be getting plugged in.

First thing:
Change it to POST
<form id="zooKeeperForm" action="zoo.php" method="POST" onsubmit="return validateForm()">
// ^ POST not GET
Second. Name attributes is the one to be used not ids
<input id="name" placeholder="Jane Smith" autofocus required>
// NOT ID but name="name"
This must be:
<input name="telephone" placeholder="(xxx) xxx-xxxx" id="telephone" />
<input name="email" type="email" required id="email" />
<input name="birthDate" type="date" required id="birthDate" />
<select name="gender" id="gender">
<textarea name="comments" oninput="validateComments(this)" id="comments" required>
Third:
The simple thing here is that, always process form input the form is submitted. Not upon initial load.
Catch the submission with something like this:
<input type="submit" name="zoo_submit" value="Submit Application" />
Then in PHP:
// simple initialization
$name = $telephone = $email = $birthDate = $gender = $comments = '';
if(isset($_POST['zoo_submit'])) {
$name = isset($_POST['name']) ? $_POST['name'] : '';
$telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$birthDate = isset($_POST["birthDate"]) ? $_POST['birthDate'] : '';
$gender = isset($_POST['gender']) ? $_POST['email'] : '';
$comments = isset($_POST['comments']) ? $_POST['comments'] : '';
}
?>
Thanks for submitting your application!<br>
The following is the information we received:<br>
<!-- now you have already set the variables above, use them, not the POST values again -->
Name: <?php echo $name; ?><br>
Telephone: <?php echo $telephone; ?><br>
E-Mail: <?php echo $email; ?><br>
Birthday: <?php echo $birthDate; ?><br>
Gender: <?php echo $gender; ?><br>
When you first wanted to be a zookeeper: <?php echo $comments; ?>

Related

Retain textarea information if page accidentally refreshes or capcha doesn't work

I have managed to find answers on the input areas for text and have that working. I have a contact form, two inputs - name & email - and a textarea for information.
Looking online I find answers to use this line of code in the value to retain the information if there is a page refresh -
<input type="text" name="name" placeholder="Your Name" value='<?php echo
isset($_POST['name']) ? $_POST['name'] : ''; ?>' required>
Although when I add the value='' to my text area it doesn't work like the others. I changed the names to match too.
Here is the physical page so you can see for yourself - idwithin.xyz/contact.php and I'll paste all my code below.
Thank you.
<div class="contactForm">
<form method="post" action="">
<input type="text" name="name" placeholder="Your Name" value='<?php echo
isset($_POST['name']) ? $_POST['name'] : ''; ?>' required>
<input type="text" name="email" placeholder="Your Email" value='<?php echo
isset($_POST['email']) ? $_POST['email'] : ''; ?>' required>
<textarea type="text" name="message" placeholder="Your Message" value='<?
php echo isset($_POST['message']) ? $_POST['message'] : ''; ?>' required>
You don't add value="" to textareas, you render the value between the tags:
<textarea><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''; ?></textarea>

The information from the form is not sending to the database

It is echoing to the other page but not sending to the database. There is a php page just for to connect to the database.So I took off the connection to the database in this php page but got alot of errors.
This is the code:
<?php
require 'login.php';
$path = 'img/';
if (isset($_POST['submit'])) {
// Grab the image from the POST array
$fn = isset($_POST['fname']) ? $_POST['fname'] : '';
$ln = isset($_POST['lname']) ? $_POST['lname'] : '';
$sex = isset($_POST['sex']) ? $_POST['sex'] : '';
$city = isset($_POST['city']) ? $_POST['city'] : '';
$em = isset($_POST['email']) ? $_POST['email'] : '';
$pass = isset($_POST['pword']) ? $_POST['pword'] : '';
$confirm = isset($_POST['cword']) ? $_POST['cword'] : '';
//$gend = $_POST['gender']; not using now
$pic = $_FILES['pic']['name'];
if (!empty($fn) && !empty($ln) && !empty($pic)) {
// Move the file to the target upload folder
$target = $path.$pic;//create image source (src)
if (move_uploaded_file($_FILES['pic']['tmp_name'], $target)) {
// // Connect to the database
$dbase = mysqli_connect('localhost', 'root', '', 'flowers');
//Write the data to the database
$my_query = "insert into members values ('$fn', '$ln', '$sex','$city','$em','$pass', '$pic');";
mysqli_query($dbase, $my_query);
// Confirm success with the user
echo '<p>Thank you for registering with us!</p>';
echo '<p><strong>Name:</strong> ' . $fn .' '.$ln .'<br />';
echo '<img src="' . $path . $pic . '" alt="profile image" /></p>';
echo '<p><< Return to home page</p>';
}
}
}
form code:
<div id="formControl">
<form id="form" action="img_upload.php" method="post" onsubmit="return validateForm(); "enctype="multipart/form-data">
<fieldset>
<legend>Personal Information</legend>
<label> First Name: </label> <br/>
<input type="text" id="fname" name="fname"/> <br/>
<span id="f_error"></span><br/><br/>
<label>Last Name </label><br/>
<input type="text" name="lname" id="lname"/><br/><br/>
<span id="l_error"></span><br/>
<label> Sex: </label><br/>
Male <input type="radio" id="msex" name="sex" value="Male"/>
Female <input type="radio" id="fsex" name="sex" value="female"/> <br/> <br/> <br/>
<label>City: </label>
<select>
<option value="" selected="selected" name="city" id="add">Select a City</option>
<option value="sando">San Fernando</option>
<option value="pos">Port of Spain</option>
<option value="chag">Chaguanas</option>
<option value="arima">Arima</option>
<option value="bella">Marabella.</option>
<option value="point">Point Fortin</option>
<option value="puna">Tunapuna</option>
<option value="scarborough">Scarborough</option>
</select>
<span id="ad_error"></span><br/><br/>
</fieldset>
<fieldset>
<legend>Register</legend>
<label>Email Address</label><br/>
<input type="text" id="email" name="email"/><br/><br/>
<span id="em_error"></span><br/><br/>
<label> Password: </label><br/>
<input type="password" id="pword" name="pword"/> <br/> <br/>
<span id="p_error"></span><br/><br/>
<label>Confirm Password: </label><br/>
<input type="password" id="cword" name="cword"/> <br/> <br/>
<span id="c_error"></span><br/>
<label>Profile Picture: </label><br/>
<input type="file" name="pic" id = "pic" /><br/> <br/> <br/>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</fieldset>
</form>
</div>
Please check $dbase link after connect to database.
after it check error code.
also you can echo your sql Query to know what sending to database and to valuate that, copy it and execute in direct in your mysql database
Make sure that the column count of your table matches with the amount of values you are inserting.
I noticed you didn't use the $gend variable, yet it may be a column in your table.
Auto-incrementing fields will be set default on +1 each new entry. I tested a similar query and I encountered the following error:
#1136 - Column count doesn't match value count at row 1

Copying a value from POST into a variable

In a page I have a form that submits with Post method. In another page I'm just trying to retreive and echo those values. If I just:
var_dump($_POST)
I see every value I entered in the form.
If I do something like:
echo $_POST['name'];
echo $_POST['surname'];
echo $_POST['telephone'];
I see the correct values.
But if, at the beginning of the page, use:
$name = $_POST['name'];
$surname = $_POST['surname'];
$telephone = $_POST['telephone'];
echo $name . ' ' . $surname . ' ' . $telephone;
I notice that for example name and telephone are always empty.
HTML form:
<form id="dom_com_sci" class="form" name="dom_com_sci" action="./ricevuta-fiscale-2convention.php" method="post" enctype="application/x-www-form-urlencoded" target="_self" style="height: 2200px; width: 619px;">
<p>
<label for="name">name:</label>
<input id="name" class="focus capitalize" name="name" type="text" size="26" value="" maxlength="70" placeholder="es. Mario" autocomplete="off" />
</p>
<p>
<label for="surname">surname:</label>
<input id="surname" class="focus capitalize" name="surname" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<p>
<label for="telephone">telephone:</label>
<input id="telephone" class="focus capitalize" name="telephone" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<input type="submit" name="Send" value="Send" tabindex="34" />
</form>
The php script on the result page:
<?php
require_once('./function/generic_function.php');
if(isset($_POST['submit']) && $_POST['submit'] == "Send") {
$surname= $_POST['surname'];
$name = $_POST['name'];
$telephone = $_POST['telephone'];
}
?>
generic_function.php:
<?php
function replace($stringa) {
$stringa_r = str_replace('\\','',$stringa);
return $stringa_r;
}
function accesso($percorso,$opzione = "N") {
($opzione == "Y") ? $blank = "target=_blank" : $blank = "";
if(isset($_SESSION['access_level']) && $_SESSION['access_level'] > 1 && isset($_SESSION['logged']) && $_SESSION['logged'] == 1 && isset($_SESSION['user_id'])) {
print('href="'.$percorso.'"'.$blank);
} else {
print('href="'.$dir.'access.php"');
}
}
?>
you are missing on quote (') in these line
$surname= $_POST['surname];
$name = $_POST['name];
$telephone = $_POST['telephone];
it should be this
$surname= $_POST['surname'];
$name = $_POST['name'];
$telephone = $_POST['telephone'];
<?php
//error_reporting($level=NULL);
?>
<form id="dom_com_sci" class="form" name="dom_com_sci" action="" method="post" enctype="application/x-www-form-urlencoded" target="_self" >
<p>
<label for="name">name:</label>
<input id="name" class="focus capitalize" name="name" type="text" size="26" value="" maxlength="70" placeholder="es. Mario" autocomplete="off" />
</p>
<p>
<label for="surname">surname:</label>
<input id="surname" class="focus capitalize" name="surname" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<p>
<label for="telephone">telephone:</label>
<input id="telephone" class="focus capitalize" name="telephone" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<input type="submit" name="submit" value="submit" tabindex="34" />
</form>
<?php
//require_once('./function/generic_function.php');
if(isset($_POST['submit']) && $_POST['submit'] == "submit") {
$surname= $_POST['surname'];
$name = $_POST['name'];
$telephone = $_POST['telephone'];
echo $name . ' ' . $surname . ' ' . $telephone;
}
?>
Comment:: To style your form. You can put it inside the div and than can give that div style.
Thanks let me know it work or not.

Can't get php contact form to work

I have a webpage that has two contact forms and the second one is working fine but I can't seem to get the first one to work. I am fairly new to php. Thanks in advance for the help. Here's the html:
<h3>Message Us</h3>
<form action="?" method="POST" onsubmit="return saveScrollPositions(this);">
<input type="hidden" name="scrollx" id="scrollx" value="0" />
<input type="hidden" name="scrolly" id="scrolly" value="0" />
<div id="msgbox1">
<label for="name2">Name:</label>
<input type="text" id="name2" name="name2" placeholder=" Required" />
<label for="email2">Email:</label>
<input type="text" id="email2" name="email2" placeholder=" Required" />
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" placeholder="" />
<label for= "topic">Topic:</label>
<select id="topic" name="topic">
<option value="general">General</option>
<option value="stud">Property Price Enquiry</option>
<option value="sale">Bull Sale Enquiry</option>
</select>
</div><!--- msg box 1 -->
<div id="msgbox2">
<textarea id="message" name="message" rows="7" colums="25" placeholder="Your Message?"></textarea>
<p id="feedback2"><?php echo $feedback2; ?></p>
<input type="submit" value="Send Message" />
</div><!--- msg box 2 -->
</form><!--- end form -->
</div><!--- end message box -->
And here's the php:
<?php
$to2 = '418#hotmail.com'; $subject2 = 'MPG Website Enquiry';
$name2 = $_POST ['name2']; $email2 = $_POST ['email2']; $phone = $_POST ['phone']; $topic = $_POST ['topic']; $message = $_POST ['message'];
$body2 = <<<EMAIL
This is a message from $name2 Topic: $topic
Message: $message
From: $name2 Email: $email2 Phone: $phone
EMAIL;
$header2 = ' From: $email2';
if($_POST['submit']=='Send Message'){
if($name2 == '' || $email2 == '' || $message == ''){
$feedback2 = '*Please fill out all the fields';
}else {
mail($to2, $subject2, $body2, $header2);
$feedback2 = 'Thanks for the message. <br /> We will contact you soon!';
} } ?>
For the saveScrollPositions I use the following php and script:
<?php
$scrollx = 0;
$scrolly = 0;
if(!empty($_REQUEST['scrollx'])) {
$scrollx = $_REQUEST['scrollx'];
}
if(!empty($_REQUEST['scrolly'])) {
$scrolly = $_REQUEST['scrolly'];
}
?>
<script type="text/javascript">
window.scrollTo(<?php echo "$scrollx" ?>, <?php echo "$scrolly" ?>);
</script>
You have a mistake in your HTML part i.e.
<input type="submit" value="Send Message" />
add attribute name also in this input type like this-
<input type="submit" value="Send Message" name="submit" />
one thing more to correct in your php script is write-
$header2 = "From: ".$email2; instead of $header2 = ' From: $email2';
now try it.
What do you return in "saveScrollPositions" function? if you return false the form submit wont fire.

wont store user input in the database using php

the problem is only the password is stored but the rest doesnt...
this is the php code
<?php
define('DB_NAME','my_db');
define('DB_USER','root');
define('DB_PASSWORD','123');
define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$firstname = isset($_POST['fname']) ? $_POST['fname'] : '';
$lastname =isset($_POST['lname']) ? $_POST['lname'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$bday = isset($_POST['bday']) ? $_POST['bday'] : '';
$gender = isset($_POST['gender']) ? $_POST['gender'] : '';
$pass = isset($_POST['password']) ? $_POST['password'] : '';
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$sql = "INSERT INTO user(fname,lname,email,bday,gender,password)
VALUES ('$firstname','$lastname','$email','$bday','$gender','$pass')";
if(!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
this is the html code
<form action="connect.php" method="post">
First name: <input type="password" name="fname">
Last name: <input type="text" name="lname"><br><br>
Email Address: <input type="email" name="email"><br><br>
Birthday: <input type="date" name="bday">
Sex: <input list="Sex" name="gender">
Password: <input type="password" name="password"><br><br>
<datalist id="Sex">
<option value="Male">
<option value="Female">
</datalist><br><br>
<input type="submit" value="Sign up!" id="btnsignup" />
</form>
*data types in the table are: fname is varchar, lname is varchar, email is varchar, bday is date, gender is char, password is char *
First of all, your HTML can only be used with the newest browsers. Try this instead to support older browsers (perhaps you've got one too and that is the problem):
<form action="connect.php" method="post">
First name: <input type="text" name="fname">
Last name: <input type="text" name="lname"><br><br>
Email Address: <input type="text" name="email"><br><br>
Birthday: <input type="text" name="bday" placeholder="YYYY-MM-DD">
Sex: <select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Sign up!" id="btnsignup" />
</form>
You can use var_dump($_POST) or print_r($_POST) in the first line before all other code in your PHP script to check if the POST input from your HTML reaches the PHP.
As a alternative you can check the output of var_dump($_REQUEST), this array will contain GET and POST variables.
Double check if there is other PHP code executed before the one you've posted (redirects, manipulating $_POST vars and so on).
Also check if there are cookies, $_SERVER or ENV variables set with the variable names you have used.
In normal circumstances your example code should work.
Please check character limit of field in table or field name in table. I think there is the problem.

Categories