Jump to content

How To Know If A Filed Is Empty And Make A Redireccion?


ranshemer
 Share

Recommended Posts

Hi i'm a newbie in this forum i like W3S is the best page ever for learning and be a web developer and i need help... how do i know if a filed is empty on php & mysql block code and make a redirection on the same page or other files for example... if we have a form called

persons.php
and have this code
<html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html>

and then we have this file call it

insert.php
<?php$con = mysql_connect("localhost","peter","abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db", $con);$sql="INSERT INTO Persons (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con)?>

and if a have fristname as a UNIQUE field in mysql... how do i know if a field is empty and make a redirection to a pages or file? (that includes same files on your root folder) i hope that you help me thx

Link to comment
Share on other sites

just check the value of the $_POST members you want to validate against. Since they come over as strings, if there was nothing submitted to them, you could use

if($_POST['firstname'] === ''){  //redirect logic here}else{  //all other code here};

  • Like 1
Link to comment
Share on other sites

ok thx i thing i have a idea how to do it ,i will show my real code: i have 2 files there name are in Spanish so sry (it is because is my native language)

  1. CrearUsario.php (Create User)
  2. proceso_usuario.php (user process) (with which make the insert to mysql)

This is CrearUsuario.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="stylesheet" href="user_estilo.css" type="text/css" /><title>Crear Usuario</title></head><body><div id="forma">	 <?php include("menu.php"); ?><h2 id="titulo_peque">cree su usario:</h2><form  name="datos" action="proceso_usuario.php" method="post" >	<p class="texto">username:<input type="text" name="username" id="username" /></p>	<p class="texto">password:<input type="password" name="password" id="password" /></p>	<p class="texto">Descripcion:</p>	<input type="text" name="descripcion" id="descripcion" />	<br />	<input type="submit" name="botonphp" value="Registrar" id="boton"/>	</form>	  </div></body></html>

This is proceso_usuario.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>proceso</title><link rel="stylesheet" href="user_estilo.css" type="text/css" /></head><body><div id="forma"><?php include("menu.php"); ?><?php$con = mysql_connect("localhost","root","1234");if (!$con)  {  die('Error en la coneccion de SQL: ' . mysql_error());  }mysql_select_db("rani", $con);$sql="INSERT INTO usuario (username, password, descripcion)VALUES ('$_POST[username]','$_POST[password]','$_POST[descripcion]')";if (!mysql_query($sql,$con))  {  die('Error con las tablas: ' . mysql_error());  }echo "Usuario Creado";mysql_close($con)?>

so it will be ... note: i what to redirect to CrearUsuario.php

<?php$sql="INSERT INTO usuario (username, password, descripcion)VALUES ('$_POST[username]','$_POST[password]','$_POST[descripcion]')";if($_POST['username'] && $_POST['password'] ==""){  //DONT KNOW THE REDIRECT CODE CANT YOU TELL}echo field can't be empty?>

Edited by ranishemer
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...