Jump to content

Change CSS via PHP?


lucy

Recommended Posts

Yes its me again!

 

Im writing some validation for a form. The form fully validates how i would like it to but there is a slight problem about notifying the user about erroneous fields.

 

Whenever there is an error the script just loads the form back up again via header( "Location: ../newcustomer.php" ); and changes the variable 'error' to whatever field number caused the problem.

 

The problem is, i do not know how i can use this numeric value to change the CSS of the file so i can make the text red or something similar, on the field which caused the problem.

 

Another problem ive just realised, is once there is a problem, and the user is sent back to the page, all of the data entered into the form has been lost.

 

Can someone please give me some help!

 

newcustomer.php (the main page to display the form):

<body>
<?php include("php/header.php"); ?>


<div id="content">
<?php include("php/newcustomerform.php"); ?>
</div>

</body>

 

 

The form itself:

<form action="php/newcustomerscript.php" method="post">
  <p>Title:
    <select name="title" id="title" tabindex="1">
      <option selected="selected"></option>
      <option>Mr</option>
      <option>Mrs</option>
      <option>Miss</option>
</select>
  </p>
  <p>Forename: 
    <input type="text" name="fname" id="fname" tabindex="2" />
  </p>
  <p>Surname: 
    <input type="text" name="sname" id="sname" tabindex="3" />
  </p>
  <p>
    Address line 1: 
    <input type="text" name="add1" id="add1" tabindex="4" />
  </p>
  <p>Address line 2: 
    <input type="text" name="add2" id="add2" tabindex="5" />
  </p>
  <p>County: 
    <input type="text" name="county" id="county" tabindex="6" />
  </p>
  <p>Postcode: 
    <input type="text" name="pc" id="pc" tabindex="7" />
  </p>
  <p>Email: 
    <input type="text" name="email" id="email" tabindex="8" />
  </p>
  <p>
    <input type="reset" name="clear" id="clear" value="Reset" />
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
  <p> </p>
</form>

 

The script which validates it:

<?




$user="OMMITED"; //specially created username
$password="OMMITED";
$database="OMMITED";

//connect to the database
$con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error());

//select the database
mysql_select_db($database, $con);

// email validation function
function email_valid ($email) { 

if (eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,6}$", $email))
    { return TRUE; } else { return FALSE; }
    
} 

//request variables from form
$title = $_REQUEST['title'];
$fname = $_REQUEST['fname'];
$sname = $_REQUEST['sname'];
$add1 = $_REQUEST['add1'];
$add2 = $_REQUEST['add2'];
$county = $_REQUEST['county'];
$pc = $_REQUEST['pc'];
$email = $_REQUEST['email'];
$error = 0;

if (empty($title)) {
    header( "Location: ../newcustomer.php" );
$error = 1;
  }
elseif (empty($fname)) {
    header( "Location: ../newcustomer.php" );
$error = 2;
  }
elseif (empty($sname)) {
    header( "Location: ../newcustomer.php" );
$error = 3;
  }  
elseif (empty($add1)) {
    header( "Location: ../newcustomer.php" );
$error = 4;
  }
elseif (empty($add2)) {
    header( "Location: ../newcustomer.php" );
$error = 5;
  }
elseif (empty($county)) {
    header( "Location: ../newcustomer.php" );
$error = 6;
  }
elseif (empty($pc)) {
    header( "Location: ../newcustomer.php" );
$error = 7;
  }
elseif (email_valid ($email) == FALSE) {
    header( "Location: ../newcustomer.php" );
$error = 8;
}

//insert data into database
if ($error == 0) {
mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error());
}

//close connection
mysql_close();

?>

Link to comment
Share on other sites

Your best bet is to submit the form to itself rather than to another script. This way you can do something like this

<?php

function set_field_value($form)
{
    return isset($_POST[$form]) ? $_POST[$form] : '';
}

function show_field_error($form)
{
    global $error;
    return isset($error[$form]) ? '<span style="color:red"><b>'.$error[$form].'</b></span><br />' : '';
}

if(isset($_POST['submit']))
{
    if(isset($_POST['name']) && empty($_POST['name']))
    {
        $error['name'] = 'Please fill in your name';
    }

     if(isset($_POST['age']) && !is_numeric($_POST['age']))
    {
        $error['age'] = 'Please fill in your age';
    }
}

?>
<form action="" method="post">
<?php echo show_field_error('name'); ?>
Name <input type="text" name="name" value="<?php echo set_field_value('name'); ?>" /><br />

<?php echo show_field_error('age'); ?>
Age <input type="text" name="age" value="<?php echo set_field_value('age'); ?>" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

Link to comment
Share on other sites

Ahh right. Thanks. Il give it a bash now.

 

Thanks,

Lucy

 

To submit the form to itself you dont set the action attribute in the form tag. As my example shows.

 

Just a quick question.  I've heard of a "PHP_SELF" function/action.  What would be the difference between just leaving action blank and using the "PHP_SELF" action?

Link to comment
Share on other sites

I understand that now, thanks a lot :)

 

nothing happens when all fields are valid though and the user clicks submit. How can i use the statement which i was previously using to enter data when there are no errors?

 

it was:

if ($error == 0) {
mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error());
}

 

Thanks,

Lucy

Link to comment
Share on other sites

I cant figure out where to put it in order to get no errors.

 

Ive tried placing it inside the

if(isset($_POST['submit']))

main set of if statements as shown here:

if(isset($_POST['submit']))
{
    if(isset($_POST['title']) && empty($_POST['title'])){
        $error['title'] = 'Please enter a title';
    }
if(isset($_POST['fname']) && empty($_POST['fname'])){
        $error['fname'] = 'Please enter your forename';
    }
     if(isset($_POST['sname']) && empty($_POST['sname'])){
        $error['sname'] = 'Please enter your surname';
    }
     if(isset($_POST['add1']) && empty($_POST['add1'])){
        $error['add1'] = 'Please enter the first line of your address';
    }
     if(isset($_POST['add2']) && empty($_POST['add2'])){
        $error['add2'] = 'Please enter the second line of your address';
    }
    if(isset($_POST['county']) && empty($_POST['county'])){
        $error['county'] = 'Please enter your county';
    }
    if(isset($_POST['pc']) && empty($_POST['pc'])){
        $error['pc'] = 'Please enter your postcode';
    }
    if(isset($_POST['email']) && empty($_POST['email'])){
        $error['email'] = 'Please enter your email';
    }
if (!$error)
{mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error());		 
}
}

 

which works ok-ish untill i try and redirect the user to another page by using

header( "Location: http://www.google.com" );

and then it comes up with the following error, just above the text boxes for input:

Warning: Cannot modify header information - headers already sent by (output started at /home/sites/foo/public_html/test/newcustomer.php:12) in /home/sites/foo/public_html/test/php/testvalid.php on line 56

which corresponds directly to the statement i have just added.

 

I cant find a suitable place to put this if statement.

 

Any ideas?

 

Thanks,

Lucy

Link to comment
Share on other sites

I cant figure out where to put it in order to get no errors.

 

Ive tried placing it inside the

if(isset($_POST['submit']))

main set of if statements as shown here:

if(isset($_POST['submit']))
{
    if(isset($_POST['title']) && empty($_POST['title'])){
        $error['title'] = 'Please enter a title';
    }
if(isset($_POST['fname']) && empty($_POST['fname'])){
        $error['fname'] = 'Please enter your forename';
    }
     if(isset($_POST['sname']) && empty($_POST['sname'])){
        $error['sname'] = 'Please enter your surname';
    }
     if(isset($_POST['add1']) && empty($_POST['add1'])){
        $error['add1'] = 'Please enter the first line of your address';
    }
     if(isset($_POST['add2']) && empty($_POST['add2'])){
        $error['add2'] = 'Please enter the second line of your address';
    }
    if(isset($_POST['county']) && empty($_POST['county'])){
        $error['county'] = 'Please enter your county';
    }
    if(isset($_POST['pc']) && empty($_POST['pc'])){
        $error['pc'] = 'Please enter your postcode';
    }
    if(isset($_POST['email']) && empty($_POST['email'])){
        $error['email'] = 'Please enter your email';
    }
if (!$error)
{mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error());		 
}
}

 

which works ok-ish untill i try and redirect the user to another page by using

header( "Location: http://www.google.com" );

and then it comes up with the following error, just above the text boxes for input:

Warning: Cannot modify header information - headers already sent by (output started at /home/sites/foo/public_html/test/newcustomer.php:12) in /home/sites/foo/public_html/test/php/testvalid.php on line 56

which corresponds directly to the statement i have just added.

 

I cant find a suitable place to put this if statement.

 

Any ideas?

 

Thanks,

Lucy

Well your thread is too long to go through right now, if you get error then you can use javascript to redirect. So instead of header( "Location: http://www.google.com" ); use

 

echo "<script>document.location.href='http://www.google.com';</script>";

exit();

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.