I am trying to post the values as shown in the script from an HTML form. I have confirmed that the form is successfully submitting the data. However, when I try to test the values being submitted using the isset() script, I get nothing at all. I am also posting my function to see if I am making an error there.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// require_once('mystuff/functions.php');
echo $to_account = test_input($_POST['to_account']);
echo $to_email = test_input($_POST['to_email']);
echo $amount = test_input($_POST['amount']);
echo $pin = test_input($_POST['pin']);
} else {
echo "Probably the data was not submitted";
}
<div class="container">
<h2>Transfer:</h2>
<form action="transCtrl.php" method="POST">
<div class="form-group">
<label for="to_account">To Account</label>
<input type="text" class="form-control" id="to_account" name="to_account" required pattern="[A-Z0-9]{13}">
</div>
<div class="form-group">
<label for="to_email">To Email:</label>
<input type="email" class="form-control" id="to_email" name="to_email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" required>
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="number" class="form-control" id="amount" name="amount" required min="100">
</div>
<div class="form-group">
<label for="pin">8 Digit PIN</label>
<input type="text" class="form-control" id="pin" name="pin" pattern="[0-9]{8}" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
Instead this
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
Use this:
if($_SERVER["REQUEST_METHOD"] == "POST") {
You don't have a $_POST['submit'] variable.
Related
it always gives me the first condition which gives me "Name Cannot be empty." and it dosen't send the data...
i tried changing the $_POST['inputs'] with variables but everytime it gives me undefined index
inside the script there's the code that sends the data to firebase
what seems to be the problem here
<form class="pb-5 ml-5" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<input type="text" class="form-control text-right" id="inputName" name="inputName">
<label for="staticEmail" class="col-sm-3 col-form-label text-left">الإسم</label>
<input type="email" class="form-control text-right" id="inputEmail" name="inputEmail">
<label for="staticEmail" class="col-sm-3 col-form-label text-left" >الإيميل</label>
<input type="password" class="form-control text-right" id="inputPassword" name="inputPassword">
<label for="staticPassword" class="col-sm-3 col-form-label text-left">كلمة المرور</label>
<input type="tel" id="inputNum" class="form-control text-right" name="inputNum">
<label for="staticNum" class="col-sm-3 col-form-label text-left" >رقم الموبايل</label>
<input type="hidden" name="form_submitted" value="1" />
<input type="button" value="اشترك" class="btn btn-danger w-50 mr-5" id="create-newuser-button" name="createUser">
</form>
</section>
</div>
<?php
$nameEmptyErr = $emailEmptyErr = $mobNumEmptyErr = $passwordEmptyErr = "";
$nameErr = $emailErr = $mobNumErr = $passwordErr = "";
$validation = true;
//Name Validation
if (empty($_POST['inputName'])) {
$nameEmptyErr = '<div class="error">
Name cannot be empty.
</div>';
echo $nameEmptyErr;
} else {
$name = test_input($_POST['inputName']);
//Email Validation
if (empty($_POST['inputEmail'])) {
$emailEmptyErr = '<div class="error">
Email cannot be empty.
</div>';
echo $emailEmptyErr;
} else {
$email = test_input($_POST['inputEmail']);
//Password Validation
if (empty($_POST['inputPassword'])) {
$passwordEmptyErr = '<div class="error">
Password cannot be empty.
</div>';
echo $passwordEmptyErr;
} else {
$password = test_input($_POST['inputPassword']);
//Mobile Number Validation
if (empty($_POST['inputNum'])) {
$mobNumEmptyErr = '<div class="error">
Mobile Number cannot be empty.
</div>';
echo $mobNumEmptyErr;
} else {
$mobNum = test_input($_POST['inputNum']);
$validation = true;
}
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['createUser']) && $validation = true) :
?>
<script type='text/javascript'></script>
<?php
endif;
?>
Your page isn't testing to see if it has been submitted. As a result it displays your form and immediately starts validating the parameters - which aren't yet there.
You also don't seem to have a submit button, so there's no way to submit the form. If you have such a button you can test for it and do the validation if you find it.
Rework your page like this:
// Add a test for the presence of the submit button.
//If not found, display a form, otherwise do the validation
if (!isset($_POST['createUser'])) {
?>
<div>
<section>
<!-- No need to specify an action if the form is submitting to the same page. -->
<form class="pb-5 ml-5" method="POST">
<input type="text" class="form-control text-right" id="inputName" name="inputName">
<label for="staticEmail" class="col-sm-3 col-form-label text-left">الإسم</label>
<input type="email" class="form-control text-right" id="inputEmail" name="inputEmail">
<label for="staticEmail" class="col-sm-3 col-form-label text-left" >الإيميل</label>
<input type="password" class="form-control text-right" id="inputPassword" name="inputPassword">
<label for="staticPassword" class="col-sm-3 col-form-label text-left">كلمة المرور</label>
<input type="tel" id="inputNum" class="form-control text-right" name="inputNum">
<label for="staticNum" class="col-sm-3 col-form-label text-left" >رقم الموبايل</label>
<input type="hidden" name="form_submitted" value="1" />
<!-- Change this element to type="submit" -->
<input type="submit" value="اشترك" class="btn btn-danger w-50 mr-5" id="create-newuser-button" name="createUser">
</form>
</section>
</div>
<?php
} else {
// Do your validation here
}
Below is the html code for my form and the php code which i am using to pass data to a class method.Now the problem that i have is that the control does not seem to enter the if loop which i concluded by testing as you can see."test0" gets printed but "test1" and other subsequent "tests" do not get printed.
<form action="" method="post" enctype=multipart/form-data>
<div class="form-group">
<label for="job name">Job name:</label>
<input type="text" class="form-control" id="jobnm" value="<?php echo $_GET['jobnm'];?>" disabled>
</div>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="mail" required>
</div>
<div class="form-group">
<label for="phone">Enter a phone number:</label><br><br>
<input type="tel" id="phone" name="phone" placeholder="+91-1234567890" pattern="[0-9]{10}" required><br><br>
<small>Format: 1234567890</small><br><br>
</div>
<label >Gender</label>
<div class="radio">
<label><input type="radio" name="optradio" value="m">Male</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio" value="f">Female</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio" value="o">Other</label>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="cvFile" required>
<label class="custom-file-label" for="customFile">Upload resume</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-danger" >Reset</button>
</form>
<?php
require_once 'db-config.php';
require_once 'classCandi.php';
echo "test0";
if(isset($_POST['submit']))
{
echo "test1";
$jobID = $_GET['jobid'];
echo "test2";
$canName = $_POST['name'];
$canEmail = $_POST['mail'];
$canPhone = $_POST['phone'];
$canRadio = $_POST['optradio'];
echo "test3";
//Upload file
$fnm = "cv/";
$cvDst = $fnm . basename($_FILES["cvFile"]["name"]);
move_uploaded_file($_FILES["cvFile"]["tmp_name"],$cvDst);
echo "test4";
$obj = new Candi($conn);
$obj->storeInfo($jobID,$canName,$canEmail,$canPhone,$canRadio,$cvDst);
echo "test5";
echo '<script language="javascript">';
echo 'alert("Submitted");';
echo '</script>';
echo "test6";
}
The below code won't be true anytime! It's because you didn't understand how $_POST works.
if(isset($_POST['submit']))
There's no input element in your frontend that has name="submit". And to see, there's none of the inputs have name attribute at all.
Instead, the better way to do is, understand how this works and change your code so that, it includes:
a name attribute for all the input and form elements.
a check on the values and not $_POST['submit']
And finally...
don't copy and paste without understanding the code.
don't check on $_POST['submit'] truthness.
Example, for $canName = $_POST['name']; to work, you need to have:
<input type="text" name="name" id="name" value="<?php echo $something; ?>" />
// ^^^^^^^^^^^
And have your attribute and values in quotes please:
enctype="multipart/form-data"
// ^ ^
I have a problem with a Registration/LogIn form submission.
I have two forms in my main php as follows:
<form role="form" action="profile.php" onsubmit="return validateForm1()" method="post" class="login-form" name="form1" id="form1">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn" id="btn1" name="btn1">Sign in!</button>
</form>
and
<form role="form" action="profile.php" onsubmit="return validateForm2()" method="post" class="registration-form" name="form2" id="form2">
<div class="form-group">
<label class="sr-only" for="form-first-name">Username</label>
<input type="text" name="form-first-name" placeholder="Username..." class="form-first-name form-control" id="form-username-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-last-name">Email</label>
<input type="text" name="form-last-name" placeholder="Email..." class="form-last-name form-control" id="form-email">
</div>
<div class="form-group">
<label class="sr-only" for="form-email">Password</label>
<input type="password" name="form-email" placeholder="Password..." class="form-email form-control" id="form-password-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-about-yourself">Confirm Password</label>
<input type="password" name="form-confirm-pass" placeholder="Confirm Password..." class="form-email form-control" id="form-password-3">
</div>
<button type="submit" class="btn" name="btn2" id="btn2">Sign me up!</button>
</form>
Then I have the profile.php as follows:
<?php
if (isset($_POST['btn2'])) {
$usr = $_POST['form-username-2'];
$pass = $_POST['form-password-2'];
$email = $_POST['form-email'];
echo $usr;
echo $pass;
echo $email;
}
?>
As far as I tried I can't get the values echoed right on the other side, there is nothing printed
I'm trying to get the values only if I press the register button.
I tried the SERVER option but it works with both buttons, but I want it to work with the second.
Could you please help me out with this?
Thank you very much (sorry if my English is not good in advance...)
EDIT:
I provide you the Javascript code as I figured out without it it works... Please tell me whats wrong with the javascript validations...
<script>
function validateForm1()
{
var name = document.forms["form1"]["form-username"].value;
var pass = document.forms["form1"]["form-password"].value;
var format = /[!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
if (pass.length < 7){
alert("Please enter at least 7 character password");
return false;
}
if (!format.test(pass)){
alert("Please enter at a symbol in password");
return false;
}
return true;
}
</script>
<script>
function validateForm2()
{
var name = document.forms["form2"]["form-username-2"].value;
var mail = document.forms["form2"]["form-last-name"].value;
var pass1 = document.forms["form2"]["form-password-2"].value;
var pass2 = document.forms["form2"]["form-password-3"].value;
var passformat = /[!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
var mailformat = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (pass1 != pass2){
alert("Confirmation password doesn't match");
return false;
}
if (pass1.length < 7){
alert("Please enter at least 7 character password");
return false;
}
if (!passformat.test(pass1)){
alert("Please enter at a symbol in password");
return false;
}
if (!mailformat.test(mail)){
alert("Please enter a valid email");
return false;
}
return true;
}
</script>
you just need to add double test :
if (isset($_POST['btn1'])) {
...
} elseif(isset($_POST['btn2'])) {
...
}
Try this :
$.validate({
lang: 'en',
modules : 'security'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
<form role="form" action="profile.php" method="post" class="login-form" name="form1" id="form1">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input data-validation="length" data-validation-length="min4" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input data-validation="length" data-validation-length="min8" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn" id="btn1" name="btn1">Sign in!</button>
</form>
<form role="form" action="profile.php" method="post" class="registration-form" name="form2" id="form2">
<div class="form-group">
<label class="sr-only" for="form-username-2">Username</label>
<input data-validation="length" data-validation-length="min4" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-email">Email</label>
<input data-validation="email" type="text" name="form-email" placeholder="Email..." class="form-email form-control" id="form-email">
</div>
<div class="form-group">
<label class="sr-only" for="form-password-2">Password</label>
<input data-validation="confirmation length" data-validation-length="min8" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-confirm-pass">Confirm Password</label>
<input type="password" name="form-password_confirmation" placeholder="Confirm Password..." class="form-password form-control" id="form-confirm-pass">
</div>
<button type="submit" class="btn" name="btn2" id="btn2">Sign me up!</button>
</form>
profile.php
<?php
if (isset($_POST['btn1'])) {
$username = $_POST['form-username'];
$password = $_POST['form-password'];
echo $username;
echo $password;
} elseif(isset($_POST['btn2'])) {
$username = $_POST['form-username'];
$email = $_POST['form-email'];
$password = $_POST['form-password'];
echo $username;
echo $email;
echo $password;
}
?>
$_POST will not contain the "submit" button value "bt1" when you submit the 2nd form, and vice-versa.
Best practice is to use a hidden field instead to determine what form you are in. For instance, use this inside the 1st form: <input type="hidden" name="which_form" value="form1"/>
and then <input type="hidden" name="which_form" value="form2"/> inside the 2nd form.
Then you can check the value of $_POST['which_form'] to determine what form was posted.
I'm working on this feature in my project where I want to add three(3) images along side other details into my database row and I want this images to be in separate columns in one row.... So far below is my code. the code is not working yet... The images and texts are not uploading to the database....
Please help me out guys. What am I doing wrong. Thanks.
Below is my code sample:
//THE PHP SECTION//
<?php
session_start();
include 'includes/config.php';
if (isset($_POST['post']) && isset($_POST['itemtype'])) {
$title = mysqli_real_escape_string($link, $_POST['title']);
$itemtype = mysqli_real_escape_string($link, $_POST['itemtype']);
$description = mysqli_real_escape_string($link, $_POST['description']);
$image1 = $_FILES['image1']['name'];
$image1_tmp_name = $_FILES['image1']['tmp_name'];
$image2 = $_FILES['image2']['name'];
$image2_tmp_name = $_FILES['image2']['tmp_name'];
$image3 = $_FILES['image3']['name'];
$image3_tmp_name = $_FILES['image3']['tmp_name'];
$price = mysqli_real_escape_string($link, $_POST['price']);
$category = mysqli_real_escape_string($link, $_POST['category']);
$name = mysqli_real_escape_string($link, $_POST['name']);
//image file directory
$target1 = 'images/user_ads/'.basename($image1);
$target2 = 'images/user_ads/'.basename($image2);
$target3 = 'images/user_ads/'.basename($image3);
if (move_uploaded_file($_FILES['image1_tmp_name'], $target1)) {
}
if (move_uploaded_file($_FILES['image2_tmp_name'], $target2)) {
}
if (move_uploaded_file($_FILES['image3_tmp_name'], $target3)) {
}
//insert into ost database
$insert = "INSERT INTO products(Title,Product_Type,Description,Image1,Image2,Image3,Price,Category,Name,PostedDate)VALUES('$title','$itemtype','$description','$target1','$target2','$target3','$price','$category','$name',NOW())";
$insertKwary = mysqli_query($link, $insert);
if ($insertKwary) {
$msg = "<div class='alert alert-danger alert-success'>Product Submitted</div>";
}else{
$msg = "<div class='alert alert-danger alert-success'>Product Not Submitted...Try again</div>";
}
}
?>
//THE HTML SECTION//
<div class="col-md-8 col-md-offset-2">
<?php if(isset($msg)) { echo $msg; } ?>
<form action="" method="POST" enctype="multipart/form-data" class="postAdForm" id="postAdForm">
<div class="form-group">
<label for="Ad_title">Item Title</label>
<input type="text" name="title" class="form-control title" id="title" required=""/>
</div>
<div class="form-group">
<label for="itemtype">Item Type</label>
<select class="form-control" name="itemtype" id="itemtype">
<option>Sale</option>
<option>Request</option>
</select>
</div>
<div class="form-group">
<label for="description">Item Description</label>
<textarea name="description" class="form-control description" id="description" rows="7" required=""></textarea>
</div>
<div class="form-group">
<label for="price">First Image</label>
<input type="file" name="image1" class="form-control image1" id="image1" required="" />
</div>
<div class="form-group">
<label for="price">Second Image</label>
<input type="file" name="image2" class="form-control image2" id="image2" required="" />
</div>
<div class="form-group">
<label for="price">Third Image</label>
<input type="file" name="image3" class="form-control image3" id="image3" required="" />
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="text" name="price" class="form-control price" id="price" required="" />
</div>
<div class="form-group">
<label for="category">Item Category</label>
<select class="form-control" name="category" id="category">
<option>Sale</option>
<option>Request</option>
</select>
</div>
<div class="form-group">
<label for="price">Name</label>
<input type="text" name="name" class="form-control name" id="name" required="" readonly="" />
</div>
<div class="form-group">
<input type="submit" name="post" class="btn btn-post post" id="post" value="POST AD" />
</div>
</form>
</div>
I've also attached an image of the error i'm getting..
image of web page showing thee errors i get..error gotten
The error is due to these lines:
if (move_uploaded_file($_FILES['image1_tmp_name'], $target1)) {
}
if (move_uploaded_file($_FILES['image2_tmp_name'], $target2)) {
}
if (move_uploaded_file($_FILES['image3_tmp_name'], $target3)) {
}
You're setting $image1_tmp_name, not $_FILES['image1_tmp_name'].
Try this:
if (move_uploaded_file($image1_tmp_name, $target1)) {
}
if (move_uploaded_file($image2_tmp_name, $target2)) {
}
if (move_uploaded_file($image3_tmp_name, $target3)) {
}
Edit: Using mysqli_error() to print the error helped solve additional issues (Mentioned in comments)
I am trying to insert the data to the database by php CodeIgniter and display the result in the same page during submit the button. Below is my code.The problem is i got an error message as Duplicate entry '104' for key 'PRIMARY'. Please help. View Page
<div class="container col-lg-4">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url();?>index.php/Welcome/gallery1" method="post">
<div class="form-group has-info">
<?php
foreach ($h->result_array() as $value){
?>
<input type="hidden" name="id" value="<?php echo $value['offer_id'];?>" >
<?php }?>
<br>
<label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
<input type="text" class="form-control col-lg-6" name="offered" id="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered']; ?>">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
<textarea id="description" name="description" class="form-control col-lg-6" rows="3" value="<?php if(isset($_POST['description'])) echo $_POST['description']; ?>" ></textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4"> <span>SUBMIT</span>
</button>
</div>
</div>
</form>
Controller page
public function gallery1()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('offered','offered','required');
$this->form_validation->set_rules('description','description','required');
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select();
$this->load->view('App_stay/pages/hotel1_galery.php',$data);
if($this->form_validation->run() == FALSE)
{
}
else
{
$data['query']=$this->Login_set->add_offer();
if($data['query']!=NULL)
{
$data['ins_msg']='data inserted';
}
else
{
$data['ins_msg']='data not inserted';
}
}
}
Model Page
public function add_offer()
{
$this->form_validation->set_message('offered','offered','required');
$this->form_validation->set_message('description','description','required');
$this->load->database();
$this->load->helper('url');
$data=array
(
'offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
$this->db->insert('offer_hotel1',$data);
}