I have this form and have issues with my $_POST data not coming through:
Form:
<form action="" id="contact-form" method="post">
<input id="nombre" size="16" type="text" placeholder="Nombre">
<input id="email" size="16" type="text" placeholder="Email">
<textarea id="texto"></textarea>
<input type="submit" value="Enviar">
PHP:
<?php
if(!empty($_POST) {
echo "YES";
} else {
echo "NO";
}
?>
It always echoes "NO" when I complete and submit all the fields.
You missed the name attribute of the input fields. Change your html and add them:
<form action="" id="contact-form" method="post">
<input id="nombre" name="nombre" size="16" type="text" placeholder="Nombre">
<input id="email" name="email" size="16" type="text" placeholder="Email">
<textarea id="texto" name="texto"></textarea>
<input type="submit" value="Enviar">
Note that the id attribute is not required. You can omit it if you don't need it for other purposes like using it in a stylesheet or in javascript. For simply sending the form just the name attribute is required
if you want a specific field, try:
if(isset($_POST['field']{0})){
If you want to check for multiple fields POST, try:
if(count($_POST)>0){
Your input should have the name attribute set, which sets them in $_POST array. id is ignored in posting forms!
<input id="nombre" size="16" type="text" placeholder="Nombre" name="nombre">
and you will get it in $_POST['nombre']
This is an alternative solution ,if you add "name" attribute to your inputs , it should work:
if (count($_POST) == 0) {
echo "YES";
}
else {
echo "NO";
}
You need to specify a variable, so $_POST['email'] for example. For all of the fields try;
if(!empty($_POST['Nombre']) && !empty( $_POST['email']) && !empty ( $_POST['texto'])) { ...
Sorry I'm on a mobile device, such a pain to get all the spelling correct.
if (empty($_POST['qty'])) {
echo "<script>alert('inputan Quality tidak boleh kosong');history.go(-1);</script>";
}
else {
...
}
so no validation error, here I use array
Related
I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>
I have two inputs with same name in my form ( I use css to display one or the other depending of the screen size ), but on the form submition, is only gets the last one's value, which is a problem to me because is can be empty if I display:none it. I figured out a trick which consists of cloning the input value which is not empty to the other, so that when I send my POST variable to PHP, any one can return the value entered.
Here's my HTML code :
<form id="form" class="form" name="postuler" method="post" action="receive-post.php" enctype="multipart/form-data" novalidate>
<div class="not-hidden">
<label for="mail">Email address <span class="required">*</span></label>
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email" id="email" maxlength="40" />
</div>
<div class="hidden-fields">
<label for="mail">Email address <span class="required">*</span></label>
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email" id="email" maxlength="40"/>
</div>
<input type="submit" name="upload" id="send" value="Submit" class="postu"/>
</form>
And here's my jQuery code :
$(document).ready(function(){
$('#envoyez').on('click', function () {
if($.trim($('.hidden-fields #email').val())==0){
$('.hidden-fields #email').val()==$.trim($('.not-hidden #email').val())
} else if ($.trim($('.not-hidden #email').val())==0){
$('.not-hidden #email').val()==$.trim($('.hidden-fields #email').val())
}
})
})
Here's my php code :
if(!empty($_POST['email'])){
var_dump($_POST['email']);
} else {
echo "Theres a problem somewhere" ;
}
The problem that I get is that if I fill the not-hidden field, on the php side I get an empty string because it always retrieve the hidden-fields value.
Hello you can try like this
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40" />
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40"/>
Then you can loop in the php script to check if the array is empty and get the populated key.
UPDATE
Here's how you can check the array and get the populated key
if (isset($_POST['email'])) {
$emails = $_POST['email'];
foreach ($emails as $key => $email) {
if (!is_null($email)) {
echo $email;
break;
}
}
}
This is basic check you can save the email in variable or perform additional checks but this is the basic to get the value
There are couple of things wrong in your jQuery code, such as:
After triming the input text field's value, you're directly comparing it with 0, which is wrong. Instead you should compare it's length with 0.
$('... #email').val()==$.trim($...), == is for comparison and = is for assignment.
So your jQuery code should be like this:
$(document).ready(function(){
$('#envoyez').on('click', function () {
if($.trim($('.hidden-fields #email').val()).length == 0){
$('.hidden-fields #email').val($.trim($('.not-hidden #email').val()));
} else if ($.trim($('.not-hidden #email').val()).length == 0){
$('.not-hidden #email').val($.trim($('.hidden-fields #email').val()));
}
})
});
With the above snippet, it doesn't matter which input text field gets filled, the other field will also get updated with the same value automatically. And once you hit the submit button, you would get just one email id with the POST request.
Another approach would be to use name attribute as name='email[]' for both of your email input fields. There's no need to use any jQuery here.
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40" />
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40"/>
This way, you can access the submitted email id(doesn't matter which input text field gets filled) in the following way,
if(isset($_POST['email'])){
$email = array_values(array_filter($_POST['email']));
if(count($email))
echo $email[0];
else
echo "Theres a problem somewhere";
}
Some thing is wrong in this code, because when i press the submit button the code inside this block of code it is not executed.
Here is the code:
if(isset($_POST["form"])) {
if (Form::isValid($_POST["form"])){
include 'config_php/insert_lead.php';
} else{
header("Location: " . $_SERVER["PHP_SELF"]);
exit();
}
}
This is a small portion of the form:
<form action="index.php?s=2468" id="multiphase" method="post" class="form-horizontal">
<fieldset>
<input type="text" class="form-control shadow" name="first_name" required="" placeholder="Voornaam:" id="multiphase-element-22">
<input type="text" class="form-control shadow" name="last_name" required="" placeholder="Achternaam: " id="multiphase-element-23">
<input type="email" class="form-control shadow" name="email1" required="" placeholder="E-mailadres:" id="multiphase-element-28">
<input type="submit" value="Submit" name="submit" class="btn btn-primary" id="multiphase-element-29">
</fieldset>
</form>
One more thing the form is generated using PFBC
There isn't a post value named 'form', so you should set one to look for on the submission.
Notice that I have also used the entire POST array in the Form::isValid() check.
PHP:
if(isset($_POST["postback"])) {
$valid = true;
if (Form::isValid($_POST)){
include 'config_php/insert_lead.php';
} else{
header("Location: " . $_SERVER["PHP_SELF"]);
exit();
}
}
HTML:
<form action="index.php?s=2468" id="multiphase" method="post" class="form-horizontal">
<fieldset>
<input type="text" class="form-control shadow" name="first_name" required="" placeholder="Voornaam:" id="multiphase-element-22">
<input type="text" class="form-control shadow" name="last_name" required="" placeholder="Achternaam: " id="multiphase-element-23">
<input type="email" class="form-control shadow" name="email1" required="" placeholder="E-mailadres:" id="multiphase-element-28">
<input type="hidden" name="postback" value="1">
<input type="submit" value="Submit" name="submit" class="btn btn-primary" id="multiphase-element-29">
</fieldset>
</form>
In your code $_POST["form"] is the fault you doing...
if you are using $_POST["form"] then it means you have named any of your input field as "form" which i dont see in your code...
so instead of $_POST["form"] you should use any of the input name you using...
I suggest to use..
$_POST["submit"] instead of $_POST["form"]
because you have already named submit input field as submit...
See if it help
Change form tor submit:
<?php
if(isset($_POST["submit"])) {
if (Form::isValid($_POST["form"])){
include 'config_php/insert_lead.php';
} else{
header("Location: " . $_SERVER["PHP_SELF"]);
exit();
}
}
The the if will be executed BUT the you will still have problems with the next if (Form::isValid($_POST["form"])) I don't know how isValid works but $_POST["form"] does not exist. You can validate all inputs one by one.
I have got my validation working but I wanted to only output it, when it has been submitted but isset nor empty seems to work for me for some reason? I am using php 5.5.3 I believe
My code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
<input type="text" placeholder="Username" maxlength="15" name="username"/><br>
<input type="password" maxlength="15" name="password1"/><br>
<input type="password" maxlength="15" name="password2" /><br>
<input type="text" placeholder="your#email.com" maxlength="25" name="email"/><br>
<input type="text" maxlength="20" name="county" /><br>
<input type="submit" value="Register" name "register"/>
</form>
<?php
//Form Validation
if(empty($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
Also, I can't remember how I would enter in the same information for that user if it validates fine?
value="<?php $_POST['stuff'] ?>
<input type="submit" value="Register" name "register"/>
^
You're missing an = there.
It should be:
<input type="submit" value="Register" name="register"/>
It's betterto use isset() instead of empty(), in this case:
if(isset($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
I'm not sure what Option A and B are, but with this code, you'll always see Option B when you load the page. The condition in your if block will execute to FALSE and hence the else block will executed. If you want to avoid this, you can use an elseif statement instead of an else.
How can I make fields still in the form fields after submitting the form and return with invalid input data?
<form action="" method="post">
<label for="cellPhoneNo">البريد الالكتروني</label>
<input type="text" name="emailAddress" class="textField"/>
<span>*</span>
<span><?php
if($emailValidator != CORRECT_VALUE)
echo $errorMessage[$emailValidator];?>
</span>
<br>
</form>
and here's where I check the input
$emailValidator=checkInput($_POST['emailAddress'],INVALID_EMAIL_ADDRESS,'/^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/');
when user submit invalid email address, the validation result shows the error message to user. How can I make the invalid error email address still in the input?
add to input
<input type="text" name="emailAddress" class="textField" value="<? echo (isset($_POST) ? $_POST['emailAddress'] : "") ?>"/>
instead of "" you can insert your default value or just leave it like this :)
Try,
<input type="text" name="emailAddress" value="<?php echo $_POST['emailAddress']; ?>" class="textField"/>
Try to use javascript to validate the email before posting it to your server-side code :)
Check this link: http://www.zparacha.com/validate-email-address-using-javascript-regular-expression/