Problem with form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Problem with form

Post by DeFacto »

Hello all
i have a problem with form, when it is submited i get empty lines in my database.

Code: Select all

 
<?php
if(isset($_POST["Submit"])) {
    check_form();
} else {
    show_form();
}
 
function check_email_address($email) {
 
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
 
    return false;
  }
 
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
     if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
      return false;
    }
  }
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { 
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; 
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
 
 
function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;
$error['vardas'] = false;
    if($_POST["vardas"]=="") {
        $error['vardas'] = true;
         $print_again = true;
    }
    $error['pavarde'] = false;
    if($_POST["pavarde"]=="") {
        $error['pavarde'] = true;
         $print_again = true;
    }
    $error['adresas'] = false;
    if($_POST["adresas"]=="") {
        $error['adresas'] = true;
         $print_again = true;
    }
    $error['miestas'] = false;
    if($_POST["miestas"]=="") {
        $error['miestas'] = true;
         $print_again = true;
    }
    $error['pastas'] = false;
    if($_POST["pastas"]=="") {
        $error['pastas'] = true;
         $print_again = true;
    }
    if(!check_email_address($_POST['el_pastas'])) {
        $error['el_pastas'] = true;
         $print_again = true;
    }
     if($print_again) {
         show_form();
 
 
       } else {
        show_form();
          
       header('Location: DB/control/insert.php'); 
       }
 
}?>
 

Code: Select all

 
<body>
<?
function error_bool($error, $field) {
         if($error[$field]) {
             print("<td style=color:red>");
         }
       // else {
           // print("<td>");
        //}
    }
    function error_text($error) {
         if($error) {
             echo '<font style="color:#FF0000" size="2px">Neteisingai užpildyt? lauk? pavadinimai pažym?ti raudonai &nbsp;</font>';
         }
       else {
           echo "";
       }
    }
 
function show_form() {
global $HTTP_POST_VARS, $print_again, $error;}
?>
<form id="form1" name="form1" method="post" action="" align="left">
  <label "<?php error_bool($error, "vardas"); ?>">Vardas
  <input style="margin-top:45px" type="text" name="vardas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["vardas"];}
      else {echo "";} ; ?>">
  </label><br/>
  <label "<?php error_bool($error, "pavarde"); ?>">Pavard?
  <input style="margin-top:10px" type="text" name="pavarde" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["pavarde"];}
       ; ?>">
  </label><br/>
  <label "<?php error_bool($error, "adresas"); ?>">Adresas
  <input style="margin-top:10px" type="text" name="adresas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["adresas"];}
       ?>">
  </label>
  <br/>
  <label "<?php error_bool($error, "miestas"); ?>">Miestas
  <input style="margin-top:10px" type="text" name="miestas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["miestas"];}
      else {echo "";}  ?>">
  </label><br/>
  <label "<?php error_bool($error, "pastas"); ?>">Pašto indeksas
  <input style="margin-top:10px" type="text" name="pastas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["pastas"];}
      else {echo "";}  ?>">
  </label><br/>
  <label "<?php error_bool($error, "el_pastas"); ?>">El. paštas
  <input style="margin-top:10px" type="text" name="el_pastas" id="textfield" value="<?php if(isset($_POST["Submit"])) { echo $_POST["el_pastas"];}
      else {echo "";}  ?>">
   </label><br/><?php error_text($error); $vardas=$_POST['vardas']; ?>
  <input style="margin-top:10px" name="Submit" type="submit" value="Patvirtinti" />
  <input name="Valyti laukus" type="reset" value="Valyti laukus" />
</form>
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem with form

Post by califdon »

If your problem is that you are inserting blank records in your database, it would help if you show us your insert.php script, since that is what interacts with the database.
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: Problem with form

Post by DeFacto »

This is insert.php code.

Code: Select all

 
<?php header("Content-Type: text/html; charset=utf-8");
include '../config/config.php';
include '../config/opendb.php';
mysql_query("SET NAMES 'utf8'");
$a=$_POST['vardas'];
$query="INSERT INTO klientai (vardas, pavarde, adresas, miestas, pastas, el_pastas)
VALUES
('$_POST[vardas]','$_POST[pavarde]','$_POST[adresas]','$_POST[miestas]','$_POST[pastas]','$_POST[el_pastas]')";
mysql_query($query)or die ('Error, insert query failed');
//echo "$a";
//echo "pavyko";
include'../config/closedb.php';
header('Location: ../../katalogas1.php');
?>
 
Everything was ok till i made that upgrade that checks form for empty inputs, after that when i fill inputs i get blank records in my database.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Problem with form

Post by andym01480 »

Put curly brackets {} around each of the $_POST variables in the query eg {$_POST['pastas']}

also echo the query before the mysql_query call just to check what is happenning
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Problem with form

Post by aceconcepts »

if you're checking whether a variable contains a value, I prefer to use empty()

Code: Select all

 
if(empty($_POST["pavarde"]))
 
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: Problem with form

Post by DeFacto »

echo of query in insert.php, looks like empty fields comes from katalogas1.php.

Code: Select all

 
INSERT INTO klientai (vardas, pavarde, adresas, miestas, pastas, el_pastas) VALUES ('','','','','','')
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Problem with form

Post by aceconcepts »

Remove the last semi-colons ";" after your else { } statement in the value"" of your input fields.
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: Problem with form

Post by DeFacto »

Same.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Problem with form

Post by aceconcepts »

You're either losing the $_POST array from

Code: Select all

 
header('Location: DB/control/insert.php');
 
or you're not using the showForm() properly.
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: Problem with form

Post by DeFacto »

how could i change it?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Problem with form

Post by andym01480 »

Using header() to call the db insert will loose the form $_POST values - it'll need to be an include!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Problem with form

Post by aceconcepts »

I think that when you request a new page i.e.

Code: Select all

 
header('Location: DB/control/insert.php');
 
you lose the previously posted array - correct me if i'm wrong.

So maybe you could use an include for your insert or simply create your insert as a function.
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: Problem with form

Post by DeFacto »

do you mean i should change

Code: Select all

 
header('Location: DB/control/insert.php');
 
with this?

Code: Select all

 
include "DB/control/insert.php";
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Problem with form

Post by onion2k »

aceconcepts wrote:if you're checking whether a variable contains a value, I prefer to use empty()

Code: Select all

 
if(empty($_POST["pavarde"]))
 
I use empty() too, but you need to be careful with it. Think about what will happen if the user needs to insert "0" (zero) as a string into a field. empty() will reject it because it's a bit stupid.
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: Problem with form

Post by DeFacto »

DeFacto wrote:do you mean i should change

Code: Select all

 
header('Location: DB/control/insert.php');
 
with this?

Code: Select all

 
include "DB/control/insert.php";
 
Thanks a lot everybody, that helpped to solve my problem :D
Post Reply