Lets say we have a regular HTML form with several input fields:
<form>
<input type="text" name="user">
<input type="email" name="email">
<input type="password" name="password">
<input type="number" name="amount">
<buttont type="submit">Send</button>
</form>
That form is transmitted and then handled in a class method:
function example($user, $password, $email, $amount) {
// Check for all input fields
return "error text";
}
So far so good - that works well if you want to display one generic error message nearby the form.
// <form> ... </form>
<span style="color:red;">Generic error message</span>
But what if you want to display multiple error messages; one for each field?
<form>
<input type="text" name="user">
<span style="color:red;">user error</span>
<input type="email" name="email">
<span style="color:red;">email error</span>
<input type="password" name="password">
<span style="color:red;">password error</span>
<input type="number" name="amount">
<span style="color:red;">amount error</span>
</form>
Is there an efficient way to achieve this? I thought about collecting error messages in an array (within the method), return the array and then check with isset() if there is an error.
$errors = example($user, $password, $email, $amount);
<form>
<input type="text" name="user">
<?php
if(isset($errors["user"])) {
?>
<span style="color:red;">user error</span>
<?php
}
?>
</form>
That attempt, however, is bloating the code. Especially when we're talking about multiple forms and tens of different methods. Is there a better solution to achieve what i want?
Related
this is a page that displays a list of creatives, and the form offers search functionality to search by job title:
if(isset($_POST['creatives-submit'])){
$job = $_POST['job-title'];
$data = \Db::Common($fms5->DBH)->getWhere("creatives", "creatives_active", "Yes"," AND creatives_job LIKE '%".$job."%'")->orderBy('creatives_name', 'asc');
}
<form method="post" name="creative-search">
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
is there anything that's obviously wrong my my code?
try changing if(isset($_POST['creatives-submit'])) to if(isset($_POST['job-title']) && !empty($_POST["job-title"])) as the form is posting the job-title value and this is the value you actually care about. (Since creatives-submit will always = Submit)
also change
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
to <input class="form-control" type="text" name="job-title" id="job-title" placeholder="Search by job title" required/>
this means the form can't be submitted unless the job-title field has a value and had the correct type of text
Below is a modification of your code that just returns what the user searched for (Since I don't have it connected to a database)
<?php
if(isset($_POST['job-title']) && !empty($_POST["job-title"])){
$job = $_POST['job-title'];
?>
<p>You Searched For <?php echo $job;?></p>
<?php
}
?>
And the form
<!-- Search Form -->
<form method="post" name="creative-search">
<input class="form-control" required="required" type="text" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
I want two forms on my page that use the same script. The first one is sent correctly, but the second is not. I'm very beginner in PHP and I have no idea how to do it. I am trying to solve this, unfortunately without results. I'm using the phpmailer.
First form:
<div id="form-main">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="company">
<input name="company" type="text" class="feedback-input" placeholder="Nazwa firmy" id="company"/>
</p>
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Imię i Nazwisko" id="name"/>
</p>
<p class="phone">
<input name="phone" type="tel" required class="feedback-input" id="phone" placeholder="Telefon"/>
</p>
<p class="email">
<input name="email" type="email" required class="feedback-input" id="email" placeholder="Adres e-mail"/>
</p>
<p class="text">
<textarea name="message" class="feedback-input" id="comment" placeholder="Opis zamówienia"></textarea>
</p>
<button type="submit" class="button-blue">Send</button>
</div>
</form>
</div>
Second form:
<div id="second-form">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Imię i Nazwisko" id="name"/>
</p>
<p class="phone">
<input name="phone" type="tel" required class="feedback-input" id="phone" placeholder="Telefon"/>
</p>
<button type="submit" class="button-blue">Send</button>
</form>
handler.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','phone','company'])->areRequired()-
>maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
$pp->attachFiles(['image']);
$pp->sendEmailTo('sadowm1990#gmail.com'); // ← Your email here
echo $pp->process($_POST);
First: I am pretty sure both of your forms are sent/submitted without any problems. Your second form misses the div closing tag </div> but that shouldn't make such problems.
By "the first one is sent correctly, but the second is not." I assume you mean that you are not getting any email on the selected address when using the second form. If that is the case then:
I am not sure how your validator functions but it looks like that your problem is the validation process.
Why? : In your php code you are validating name, email, phone, company, message but only your first form have chance to satisfy those because it has those fields. Your second form doesn't have all those fields which have to be validated, so, your validation is going to fail. As consequence you are not going to receive any email (if the validator functions in that way).
In order to make your second form to function on this php code you have either to:
1. differentiate those two forms in your php code and apply the correct validation on them or
2. add missing fields to your second form (you can still make them hidden using html/css, but you need to set their default values and submit them altogether, I don't recommend doing this).
when form field is fixed like
<form name="" action="">
<input type="text" name="username">
<input type="text" name="password">
<input type="email" name="email">
<input type="submit" value="submit">
</form>
then we collect data from this from using
$input_data = Input::all(); this code and insert into particular username and email field in database
but
if form will be like
<form name="" action="">
<input type="text" name="username">
<input type="text" name="password">
<input type="email" name="email">
<input type="email" name="email_1">
<input type="email" name="email_2">
<input type="email" name="email_3">
......
......
......
......
<input type="email" name="email_15">
<input type="submit" value="submit">
</form>
then
in controller $input_data = Input::all();
get the all value but my specific question is how to separate all emails and username,password value
my db structure is [ username, password, emails ] i want to save all emails in emails field
NB: emails are dynamically added form by javascript.so all time total email number is not same
I am not 100% sure I understand what you are asking and also not familiar with Laravel, but if you are trying to make it easier on yourself with multiple form inputs of the same type without having to worry about unique names, try using array fields:
<form name="" action="">
<input type="text" name="username">
<input type="text" name="password">
<input type="email" name="email[]">
<input type="email" name="email[]">
<input type="email" name="email[]">
<input type="email" name="email[]">
<input type="email" name="email[]">
<input type="submit" value="submit">
</form>
It will just come out as a single email array that you can easily iterate through with a foreach. If you are trying to save a series of emails into one column, you can implode() the email array (though storing a bunch of emails in a comma separated string in one column is probably not the best idea).
Anyway, I am not sure if this helps at all...you may need to clarify if what I am saying is off-base.
Hi i am getting error while putting validations for email in PHP5.2
see my below code: i have called checkemail() and written function checkemail() in script. but i am getting error like Error: Duplicate entry '' for key 'PRIMARY' as i have declare email id as primary key. when i put anything in email id then it accepting in database. i think function is not executing properly. guide if have suitable solutions.if required any details then let me know.
<head>
<script>
function checkEmail()
{
$strEmail= mysql_real_escape_string($_POST['email']);
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*#[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $strEmail))
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body>
<div class="container">
<section id="content">
<form name="form1" method="post" action="check_register.php">
<div>
<input type="text" placeholder="Enter username" required="" id="uname" name="uname"/>
</div>
<div>
<input type="text" placeholder="Enter Email-id" required="" id="email" name="email" onfocus="checkEmail('email');" />
</div>
<div>
<input type="password" placeholder="Enter Password" required="" id="pass" name="pass"/>
</div>
<div>
<input type="password" placeholder="Repeat Password" required="" id="rpass" name="rpass" onfocus="checkPassword(document.getElementById('pass'), this);" oninput="checkPassword(document.getElementById('pass'), this);"/>
</div>
<div>
<input type="text" placeholder="Enter country" required="" id="country" name="country"/>
</div>
<div>
<input type="submit" value="Register" />
</div>
</form>
</section>
</div>
<script src="form-validation.js"/></script>
</body>
Do you check if the mail already exists in the database before trying to insert it ? I think you problem is here. No matters html5 or validation of the email.
And by the way, please never use <script> balise to execute php code. Use <?php /* your code here */ ?> instead.
You can also use a built-in PHP function to validate your email using filter_var, check the first example: http://www.php.net/manual/en/filter.examples.validation.php
yeah why dont use filter_var,
filter_var ($isEmail, FILTER_VALIDATE_EMAIL);
And eregi (Posix Regex) has been deprecated as PHP 5.3.0. php-manual
I hope someone can help me. I'm a little at loss on how I can achive this: I'm trying to pass infos generated in my php to another form made in Javascript. For example, if I put my first name in the input field and click submit, then it would go to another page with the actual form and have the first name already filled there.
One thing that I did notice was that the javascript form isn't within the <form> tag, it's in a bunch of tables with some input fields. I can post what it looks like in pastebin if this can help in understanding what I mean.
Also with this script im unable to edit it, I did not make it, it is one of those script you just place on your site thats auto generated.
My form looks like this:
<form action="auto-form/index.php" method="post" name="openleads" onsubmit="return checkForm(this);">
<label for="first">First Name <span class="required">*</span></label>
<input id="first_name" type="text" name="first" applicationforms="true" /><br>
<label class="error" for="name" id="first_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Zip <span class="required">*</span></span>
<input id="zip" type="text" name="zip" applicationforms="true" /><br>
<label class="error" for="name" id="zip_error" style="color:#F00; font-size:11px;">This field is required.</label>
<label for="last">Last Name <span class="required">*</span></label>
<input id="last_name" type="text" name="last" applicationforms="true" /><br>
<label class="error" for="name" id="last_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Email <span class="required">*</span></span>
<input id="email" type="text" name="email" applicationforms="true" /><br>
<label class="error" for="name" id="email_error" style="color:#F00; font-size:11px;">This field is required.</label>
<input class="button" type="submit" name="send" value="Send" />
</form>
Any help is appreciated; like I said, I'm a bit at loss on what to do with this one.
php-form.php
<form action="javascript-form.php" method="post">
<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
javascript-form.php
<form action="" method="">
<input type="text" name="name" value="<?= (isset($_POST['name'])?htmlentities($_POST['name'],ENT_QUOTES):''); ?>" />
<input type="submit" value="Submit" />
</form>
Use PHP to output the POSTed values in to the value attribute of the form fields. You can also use GET variables and use javascript to parse the window.location and scrape those form values.
this seemed to get this done
<script type="text/javascript" src="http://jquery.offput.ca/js/jquery.timers.js"></script>
<script>
$(document).everyTime(1000,function(i){
if($('#ui-datepicker-div').length>0)
{
$('#first_name').val('<?php echo $_POST['first_name']; ?>');
$('#last_name').val('<?php echo $_POST['last']; ?>');
$('#zip').val('<?php echo $_POST['zip']; ?>');
$('#email').val('<?php echo $_POST['email']; ?>');
}
})
$('#first_name').val('test');
</script>