Validation of Email in PHP 5.2? - php

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

Related

form/input field-specific validation in object methods (php)

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?

My form doesn't appear in website [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have a question here. Somehow my created registration form doesn't appear on the website. When I am deleting all the include files, it shows, but with the files - it does not. Could someone help me to solve this problem? What I'm doing wrong?
Here is my register.php file:
<!DOCTYPE html>
<?php
include 'classes/connection.php';
include 'classes/register.php';
$connection = new Connection();
$users = new Users($connection);
$users->insertUserValues();
?>
<html>
<head>
<meta charset="UTF-8">
<title>
</title>
</head>
<body>
<div class="form">
<form method ="post" action="register.php">
Vardas:<br>
<input type="text" name="name" id="name" placeholder="Vardas" required>
<br>
Pavardė:<br>
<input type="text" name="surname" id="surname" placeholder="Pavardė" required>
<br>
Prisijungimo vardas:<br>
<input type="text" name="username" id="username" placeholder="Prisijungimo vardas" required>
<br>
Slaptažodis:<br>
<input type="password" name="password" id="password" placeholder="Slaptažodis" required>
<br>
Patvirtinti slaptažodį:<br>
<input type="password" name="confirm_password" id="confirm" placeholder="Slaptažodis" required>
<br>
El. pašto adresas: <br>
<input type="email" name="email" id="email" placeholder="El. pašto adresas" required>
<br><br>
<input type="submit" name="submit" id="submit" value="Registruotis">
</form>
</div>
</body>
The link I'm entering to the registration form is correct, I've also tried to go from index.php, but it's the same..
Without checking if $_POST['submit'] was set (or any other post var), you immediately call $users->insertUserValues();
What happens inside that? Can we see? I would suggest an if statement to check the form was posted before trying to process non-existent data.

No email received when sent with contact form

When I am filling out the contact form on the website that I am making, the e-mail will be sent, but I am not receiving it in the inbox of my computer.
The code looks like this:
HTML:
<div id="form">
<form action="mailto:psteintj#xs4all.nl" id="contactForm" method="post">
<span></span>
<input type="text" name="name" class="name" placeholder="Enter your name" tabindex=1 />
<span></span>
<input type="text" name="email" class="email" placeholder="Enter your email" tabindex=2 />
<span id="captcha"></span>
<input type="text" name="captcha" class="captcha" maxlength="4" size="4" placeholder="Enter captcha code" tabindex=3 />
<span></span>
<textarea class="message" placeholder="Enter your message" tabindex=4></textarea>
<input type="submit" name="submit" value="Send e-mail" class="submit" tabindex=5>
</form>
</div>
JS:
if ((captchaVal == captchaCode) && (emailFilter.test(emailText)) && (nameFilter.test(nameText)) && (messageText > 50)) {
$.post("mail.php", {
name: $(".name").val(),
email: $(".email").val(),
message:$(".message").val()
});
$("#contactForm").css("display", "none");
$("#form").append("<h2>Message sent!</h2>");
return false;
}
and PHP:
<?php
$name = $POST['name'];
$email = $_POST['email'];
$message = $POST['message'];
Could someone tell me where I am going wrong?
Well, you are not sending any emails (or at least you haven't posted any code about it), so of course you are not receiving any emails. You should configure the mailing, and use the mail function.
The function needs a working SMTP server to actually send out the e-mail.
Your PHP has no mail() call or similar? I'm probably missing something here.

Form information not being 'seen' by MySQL

I've made a new log in / register template which uses CSS3 and HTML, yet I had a working form, but very basic, before this. So I decided to make another 'form' which should look like this:
http://www.script-tutorials.com/css 3-modal-popups/
Now, I use this form to handle my registration:
<!-- Popup Form #2 -->
<div class="popup">
<h2>Sign Up</h2>
<p>
Please enter your details here.
</p>
<div>
<form action="register.php" action="post">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Max 32 characters" name="username" />
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" placeholder="Password" name="password" />
</div>
<div>
<label for="lastname">Lastname</label>
<input type="text" id="lastname" placeholder="Lastname" name="lastname" />
</div>
<div>
<label for="email">E-mail</label>
<input type="email" id="email" placeholder="example#hotmail.com" name="email" />
</div>
<input type="submit" value="Join the Community" /> or
</form>
Yet when I test this form, it will say the data is stored (My register.php file comes with a if-statement), yet it displays nothing. (No username, (password still gets hashed / stored, though I'm not sure if it's really handling it), last name and e-mail.
I've checked and double checked the 'name' attributes' value in the register.php file. And those are correct.
Pretty sure you are accessing your form values using $_POST. The problem is that in your form tag you are setting action twice, one of which is correct i.e. action="register.php" and the other action="post" is incorrect.
Try:
<form action="register.php" method="post">
and remember to enclose variables in quotes, for example
$foo = $_POST['foo'];
$bar = $_POST['bar'];
$sql_query = "INSERT INTO my_table (foo, bar) VALUES ('$foo', '$bar');";

Passing php form info to javascript form

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>

Categories