I know I must be doing some small mistake somewhere but its been last 4 hours and I'm still scratching my head as to where is the mistake hence help would be greatly appreciated.
So I've a form in php and I try submitting it to itself. Basically my form is in register.php and it includes register.inc.php file through
include_once 'register.inc.php';
There is another page namely home.php which also includes register.inc.php and does its form processing from there. Though I've different criteria to differentiate from which page is the page register.inc.php called but for clarifications, I'll add codes from both.
Here is my home.php.
<form action = "<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method = "post" name = "key">
Enter the Unique (10 letter) you received to proceed : <input type = "text" name = "key" id = "key"></br>
<input type="submit" value="Submit" /> <?php echo $err; ?>
esc_url() is custom built function and works fine.
Here is my register.php file.
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>"
method="post"
name="registration_form">
Full Name: <input type='text'
name='fullname'
id='fullname' value = "<?php echo $fullname; ?>" disabled/><br>
Email: <input type="text" name="email" id="email" value = "<?php echo $email; ?>" disabled/><br>
Username: <input type='text'
name='username'
id='username' /><br>
Password: <input type="password"
name="password"
id="password"/><br>
Confirm password: <input type="password"
name="confirmpwd"
id="confirmpwd" /><br>
Project Name: <input type="text"
name="project"
id="project" value = "<?php echo $project; ?>" disabled/><br>
Phone Number: <input type="text"
name="phone"
id="phone" value = "<?php echo $phone; ?>" disabled/><br>
<input type="button"
value="Register"
onclick="return regformhash(this.form,
this.form.fullname,
this.form.project,
this.form.phone,
this.form.username,
this.form.email,
this.form.password,
this.form.confirmpwd);" />
</form>
I've a js file named forms.js which does encryption of password in client side before sending it to server. So here is my js file just in case required. (Though it works fine. There is no problem due to it. I've verified it.)
function regformhash(form, uid, , fullname, email, phone, project, password, conf) {
var p = document.createElement("input");
// Add the new element to our form.
form.appendChild(p);
p.name = "p";
p.type = "hidden";
p.value = hex_sha512(password.value);
// Make sure the plaintext password doesn't get sent.
password.value = "";
conf.value = "";
// Finally submit the form.
form.submit();
return true;
}
And finally, here is the code snipet from my register.inc.php file. I think there is some error in this file. I'm posting it in two parts.
The following part is condition used to check if the register.inc.php is called from home.php
if(isset($_POST['key'])) {
//Something....
}
And the following to check if its called from register.php
echo $_POST['username']. $_POST['project']. $_POST['p'];
if (isset($_POST['fullname'], $_POST['project'], $_POST['phone'],$_POST['username'],$_POST['email'], $_POST['p'])) {
// Sanitize and validate the data passed in
echo "12";
//something...
}
I start at home.php. Enter the key. Check weather it exists in database or not. If true, I redirect my user to register.php. There he gets to fill out the form. Some of details like full name, phone, email etc are filled out earlier. Hence those text fields are locked. The user needs to fill out only username and password. After he presses submit, I verify all credentials and if everything checks out I save it in DB. DB work is done in register.inc.php.
But my error is that, though my key verifies out and I get redirected to register.php, but after that when I enter my username & password, is always says :
Undefined index: username
Undefined index: project
Undefined index: p
And the array $_POST[] in register.inc.php(before isset(......)) remains undefined. I also tried GET method but the same error.
I dont know what I am doing wrong hence help will be greatly appreciated.
Thanks in advance.