PHP form verification doesn't work [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to make a html form with php verification but trying to submit the form with at least one filled textfield will say that the emails dont match (self made part of the code that should not be displayed.
<?php
error_reporting(0);
if(isset($_POST["submit"])){
//File Verification
if(empty($_POST['username']) && empty($_POST['password1']) && empty($_POST['password2']) && empty($_POST['email1']) && empty($_POST['email2']) && empty($_POST['bday'])){
echo"Kom op, vul alles in";
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
exit();
}
else{
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['password1'];
$pass2 = $_POST['password2'];
if(email1 == email2){
if(pass1 == pass2){
}
else{
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
echo"Je wachtwoorden komen niet overeen";
exit();
}
}
else{
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
echo "Je email gegevens komen niet overeen";
exit();
}
}
}
else{
$form = <<<EOT
<form method="post" action="register.php">
Gebruikersnaam: <input type="text" name="username" placeholder="type hier je gebruikers naam"/><br /><br />
wachtwoord: <input type="password" name="password1" placeholder="type hier je wachtwoord"/><br /><br />
wachtwoord opnieuw: <input type="password" name="password2" placeholder="type je wachtwoord opnieuw in"/><br /><br />
email: <input type="text" name="email1" placeholder="type hier je email"/><br /><br />
email opnieuw: <input type="text" name="email2" placeholder="type hier je email opnieuw"/><br /><br />
Geboorte datum: <input type="date" name="bday"/ placeholder="type je geboorte datum hier"><br /><br />
<input type="submit" name="submit"/>
</form>
EOT;
echo $form;
}
?>
It just shows
else{
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
echo "Je email gegevens komen niet overeen";

Try changing:
if(email1 == email2){
if(pass1 == pass2){
to:
if($email1 == $email2){
if($pass1 == $pass2){

You've a typo pass1 == pass2.
Also, you may want to change from:
if(empty($_POST['username']) &&...
to
if(empty($_POST['username']) OR...

Related

Crud application wont instert into database

my application wont insert into database, and its not giving me any errors.
If my question is a duplicate, please redirect me to the original question, thanks.
MY PHP CODE:
<?php
include_once("dbconfig.php");
if(isset($_POST['submit'])){
/*$targetDir = "slike/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';*/
$image = $_FILES['files']['tmp_name'];
$file = addslashes(file_get_contents($image));
$name = mysqli_real_escape_string($mysqli, $_POST['ime']);
$group=mysqli_real_escape_string($mysqli,$_POST['grupa']);
$semi=mysqli_real_escape_string($mysqli,$_POST['podgrupa']);
$price=mysqli_real_escape_string($mysqli,$_POST['cena']);
if(empty($name) || empty($group) || empty($semi)||empty($price)) {
echo "<font color='red'>Namesteno je da sve moraju da budu popunjene.</font><br/>";
if(empty($name)) {
echo "<font color='red'>Ime je prazno.</font><br/>";
}
if(empty($group)) {
echo "<font color='red'>Grupa je prazna.</font><br/>";
}
if(empty($semi)) {
echo "<font color='red'>Podgrupa je prazna.</font><br/>";
}
if(empty($price)){
echo "<font color='red'>Cena je prazna.</font><br/>";
}
}
else{
$result = mysqli_query($mysqli, "INSERT INTO slike(ime,grupa,podgrupa,cena,slika) VALUES('$name','$group','$semi','$price','$file')");
echo "<font color='green'>Data added successfully.";
}
}
?>
My html:
</div>
<div class="content">
<form action="" method="post" enctype="multipart/form-data">
Slike biras ovde:
<input type="file" name="files">
<p>IME</p>
<input type="text" name="ime">
<p>GRUPA</p>
<input type="text" name="grupa">
<p>PODGRUPA</p>
<input type="text" name="podgrupa">
<p>CENA</p>
<input type="text" name="cena">
<input type="submit" name="submit" value="UPISI">
</form>
If you need some more codes i will post it in replies.
Problem is that i get the message "Data added successfully"
But when i go to the database, its empty.
THANKS to anyone who helps me.
<form action="/your_php_file.php" method="post" enctype="multipart/form-data">
Please set action to your form which should go to your php file.

Load a website in a specific data-section after press submit

There is the problem:
I have a website that contains all the sections in just a page, so it is large website, I have a formulary with a reCaptcha almost at the buttom of the website, so when people don't enter the good captcha and press submit it loads the website again but it goes to the top of the website so people get lost becouse they don't know if the mail was sended or what as they have to scroll down for see the warning message that they didn't put the correct captcha.
The question, is there a way that when they press the submit it loads the website in a specific section? in this case "menu3" section
This is what I have:
<section id="menu3"> </section>
<form action="?done=1" method="post" name="myphpform" id="myphpform" >
<input type="text" id="nombre" class="form-control" name="nombre" placeholder="Escribe tu nombre" required value="<?php if (!empty($_POST['nombre'])) { echo $_POST['nombre']; } ?>" />
<input type="text" id="telefono" class="form-control" name="telefono" placeholder="Teléfono y/o celular" value="<?php if (!empty($_POST['telefono'])) { echo $_POST['telefono']; } ?>" />
<input type="email" id="email" class="form-control" name="email" placeholder="Escribe tu e-mail" required value="<?php if (!empty($_POST['email'])) { echo $_POST['email']; } ?>" />
<textarea name="comentarios" id="comentarios" class="form-control" placeholder="Comentarios y/o preguntas" required /><?php if (!empty($_POST['comentarios'])) { echo $_POST['comentarios']; } ?></textarea>
<input name="submit" type="submit" class="btn btn-info" value="Enviar" />
</form>
and the php:
<?php
#require_once('recaptchalib.php');
$publickey = "";
$privatekey = "";
$resp = null;
$error = null;
if ($_POST["submit"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$to="mail#mail";
$subject="Contacto";
$body="Contacto:
Nombre: " .$_POST["nombre"] . "\n
Empresa: " .$_POST["empresa"] . "\n
Ubicacion: " .$_POST["ubicacion"] . "\n
Telefono: " .$_POST["telefono"] . "\n
E-mail: " .$_POST["email"] . "\n
Comentarios: " .$_POST["comentarios"] . "\n";
mail($to,$subject,$body, null, '-fmail#mail.mx');
$resultMenuUrlName = "contacto_gracias.html";
echo "<META HTTP-EQUIV=Refresh CONTENT=0;URL=$resultMenuUrlName>";
exit(1);
} else {
echo "Lo sentimos pero no ha colocado el texto correctamente! Intente nuevamente...";
}
}
echo recaptcha_get_html($publickey, $error);
?>
thx in advice
You can use a named anchor:
then make the url: http://myurl.com#value
well I finally found how to go to the section after all the website is loaded as at the beginning my site have a little animation, maybe is not the best solution but it works for me. I just add it to the php:
echo "<script type=''>
setTimeout(function() {
window.location = '#anchor';
}, 7000);
</script>";

Fill in form in PHP

So I've had to make a simple phonebook in PHP, but right now it simply echos the text beneath the form, I however, want it to fill it in the form which says: Phonenumber (When you type in the exact name of someone you it echos their phonenumber)
Here's the Form:
<form action="" method="post">
Naam: <br><input type="text" name="name" /><br>
Telefoonnummer: <br><input type="text" name="phonenumber" disabled />
<input type="submit" value="submit" />
</form>
and here's the PHP (I'm Dutch btw so some of the text is in Dutch):
<?php
if(isset($_POST['name'])) {
$formNaam = $_POST['name'];
$naamPersoon = array ("Ilja Clabbers","Piet Paulusma","Gerrit Zalm");
$telefoonNummer = array ("038-4699776","0568-121212","010-2311512");
if(empty($formNaam)) {
echo 'Vul een veld in.';
} else if ($formNaam == $naamPersoon[0]){
echo "Het telefoonnummer van " . $naamPersoon[0] . " is " .$telefoonNummer[0];
} else if ($formNaam == $naamPersoon[1]){
echo "Het telefoonnummer van " . $naamPersoon[1] . " is " .$telefoonNummer[1];
} else if ($formNaam == $naamPersoon[2]){
echo "Het telefoonnummer van " . $naamPersoon[2] . " is " .$telefoonNummer[2];
} else {
echo "Deze naam staat niet in het archief";
}
}
?>
So basically what I'd like to know is; How do you get the phonenumber belonging to a persons name to be shown in the Form where it says 'Telefoonnummer:'?
A neater solution would be to make a single associative array with key => value pairs:
$naamPersoon = array (
"Ilja Clabbers" => "038-4699776",
"Piet Paulusma" => "0568-121212",
"Gerrit Zalm" => "010-2311512",
);
Then your code would be:
$phonenumber = '';
if (array_key_exists($formNaam, $naamPersoon)) {
echo "Het telefoonnummer van " . $formNaam . " is " . $naamPersoon[$formNaam];
$phonenumber = $naamPersoon[$formNaam];
} else {
echo "Deze naam staat niet in het archief";
}
Alternatively you could keep your two arrays as they are and use array_search to find the index of the name in the first array, then use it as the index you check in the second array
$phonenumber = '';
$index = array_search($formNaam, $naamPersoon);
if ($index === false) {
echo "Deze naam staat niet in het archief";
} else {
echo "Het telefoonnummer van " . $formNaam . " is " . $telefoonNummer[$index];
$phonenumber = $telefoonNummer[$index];
}
Either way you can then use the assigned variable $phonenumber to add the number to the form by echoing it out as the input's value. At which point you can take out the echos if you wish.
<form action="" method="post">
Naam: <br><input type="text" name="name" /><br>
Telefoonnummer: <br><input type="text" name="phonenumber" value="<?=htmlspecialchars($phonenumber)?>" disabled />
<input type="submit" value="submit" />
</form>

PHP Unexpected T_VARIABLE

I'm making a basic lotery script and i'm getting the same error the whole time: Unexpected T_Variable on line 5. Here is my script, I hope someone can help me:
<?php
$invulcijfer = '';
if (isset($_POST['sumbitBtn']))
{
$invulcijfer = $_POST['cijfer'];
$pinda = preg_replace("/[^0-9]/", "", $invulcijfer);
$lotnummer = "1234"; // Hier je 4 cijfers voor lotnummer
if($invulcijfer = '') {
echo "<font color='#FF000'>Je moet alles invullen</font>";
} else if($pinda !== $invulcijfer) {
echo "<font color='#FF000'>Dat zijn geen cijfers</font>";
} else {
if ($pinda == $lotnummer) {
     echo "<font color='green'>WAUW! Het is je gelukt!</font>";
} else {
echo "<font color='#FF000'>Sorry, het is niet gelukt..</font>";
// Maybe update query van dat ze - points hebben ofso? q wat jij wilt
}
}
}
}?>
<br><br>
<h3>Loterij Script</h3>
<font color="green">Typ 4 cijfers in en misschien win jij!</font><br><br>
<form action="" method="post">
<input type="text" id="naam" name="naam" maxlength="4"/><br>
<input type="text" id="cijfer" name="cijfer" maxlength="4"/><br>
<input type="submit" id="submitBtn" name="submitBtn" value="Check je lot"/>
</form>
EDIT
I spotted a few errors:
THIS:
if (isset($_POST['sumbitBtn']))
it needs to read as
if (isset($_POST['submitBtn']))
there was a spelling mistake.
Also if($invulcijfer = '') { needs to be if($invulcijfer == '') {
You have one closing brace too many.
Remove the one this one in }?> and your script will work.
This is the code that I ran, deleting the extra closing brace.
EDIT #2 (fixed conditions and spelling mistake for submit button.
<?php
$invulcijfer = '';
if (isset($_POST['submitBtn']))
{
$invulcijfer = $_POST['cijfer'];
$pinda = preg_replace("/[^0-9]/", "", $invulcijfer);
$lotnummer = "1234"; // Hier je 4 cijfers voor lotnummer
if($invulcijfer == '') {
echo "<font color='#FF000'>Je moet alles invullen</font>";
}
elseif ($pinda !== $invulcijfer){
echo "<font color='#FF000'>Dat zijn geen cijfers</font>";
} else {
if ($pinda == $lotnummer) {
echo "<font color='green'>WAUW! Het is je gelukt!</font>";
}
else {
echo "<font color='#FF000'>Sorry, het is niet gelukt..</font>";
// Maybe update query van dat ze - points hebben ofso? q wat jij wilt
}
}
}
?>
<br><br>
<h3>Loterij Script</h3>
<font color="green">Typ 4 cijfers in en misschien win jij!</font><br><br>
<form action="" method="post">
<input type="text" id="naam" name="naam" maxlength="4"/><br>
<input type="text" id="cijfer" name="cijfer" maxlength="4"/><br>
<input type="submit" id="submitBtn" name="submitBtn" value="Check je lot"/>
</form>

Mail form through php

I have made this form and I want to ask is this safe enough. I tried many times making a captcha thingie but it won't work for me. I am still a student please don't sent to hard things.
Question 1 : Is mysql_real_escape_string safe enough?
Question 2 : I need a really simple (numeric) captcha, can someone send me an example (or other stack post)
This gona be used on an informatic site just as a mail form. on that site are no databases/logins and that.
<?php
include '../connect.php'; #db connection for mysql_real_escape_string
$errors = array('');
//valideren of er op de submit gedrukt is en of alle benodigde data is ingevuld
if(isset($_POST['submit'])){
if(!empty($_POST['naam']) && !empty($_POST['email']) && !empty($_POST['bericht'])){
$naam = mysql_real_escape_string($_POST['naam']);
$email = mysql_real_escape_string($_POST['email']);
$bericht = mysql_real_escape_string($_POST['bericht']);
$telefoon = mysql_real_escape_string($_POST['telefoon']);
$regex = "/^[A-Za-z .'-]+$/";
if(!preg_match($regex,$naam)) {
array_push($errors , 'De naam is niet geldig');
}
if(strlen($bericht) < 5) {
array_push($errors , 'Het bericht is te kort');
}
$email_regex = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_regex,$email)) {
array_push($errors , 'Uw email is niet geldig.');
}
Here comes mail part.
}else{
array_push($errors , 'Een van de verplichte velden is niet ingevuld. Alle velden met * zijn verplicht.');
}
}
?>
<form method="post">
<p>
<label>naam*</label>
<input type="text" name="naam"/>
</p>
<p>
<label>email*</label>
<input type="text" name="email"/>
</p>
<p>
<label>telefoon</label>
<input type="text" name="telefoon"/>
</p>
<p>
<label>Bericht*</label>
<textarea name="bericht" style="width:459px; height:187px;" ></textarea>
</p>
<p>
<label> </label>
<input type="submit" value="verstuur" name="submit"/>
</p>
</form>
<?php
if (count ($errors > 0)){
foreach($errors as $error){
echo '<p class="error">'.$error.'</p>';
}
}
?>
Is mysql_real_escape_string safe enough?
It is a poor solution for protecting a MySQL database.
It is completely inappropriate for sending email.
Whatever you do to protect bad data from corrupting your email, it should be done just before the data is inserted into that email — not before you run sanity checks over it.

Categories