www.webdeveloper.com
Results 1 to 6 of 6

Thread: On Page Alert v Go Back

  1. #1
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64

    Arrow On Page Alert v Go Back

    My contact form validates in a basic php way as shown at www.webitry.net/contact.html
    How could I go about having the validate alerts, to present at/on the Contact Page so that visitors do not have to use Go Back ?

  2. #2
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Some additional info further to my earlier submission.

    This is the code that I have been using to work the Go Back validate requirement in the Contact Form!

    if(empty($visitor)
    || empty($visitormail)
    || empty($visitorphone)
    || empty($notes ))
    {
    echo "<h2>Go Back and fill in all fields </h2>\n";
    echo '<a href="javascript:history.go(-1)"><h3>Go Back to the Contact Form</h3></a><br />'; /*GO BACK BUTTON*/
    die ("Use the Go Back to Contact Form button !! ");
    }

    The question being..... what upgrade is needed to this code to present the alerts on the Contact page?

  3. #3
    Join Date
    Dec 2010
    Posts
    59
    Have a look at jQuery and the form validation tools for jQuery. Remember, you need to have both validations - javascript to ensure that there are visual reminders for the user to fill in forms in a certain format, and server side validation to ensure they're not trying to do anything nasty to your server.

  4. #4
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Quote Originally Posted by ralphchadkirk View Post
    ........and server side validation to ensure they're not trying to do anything nasty to your server.
    What nasty things might they try to do?
    What nasty things should we be trying to prevent?
    Is this something to do with sanitizing and what is sanatizing?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    One common strategy is to have the form and the form processing in the same file, so that the action attribute of the <form> tag points to itself:
    HTML Code:
    <form method='post' action=''>
    Then you do a check to see if the form was submitted, and if so process it, else, display the form. If there are any errors, display the form instead of continuing to process the inputs.
    PHP Code:
    <?php
    $errors 
    = array();
    if(isset(
    $_POST['submit'])) {  // or whatever other form field you want to use
       // sample validation:
       
    if(!isset($_POST['name']) or trim($_POST['name'] === '') {
          
    $errors[] = "You must enter your name.";
       }
       if(!isset(
    $_POST['email']) or trim($_POST['email'] === '') {
          
    $errors[] = "You must enter your email.";
       }
       
    // see if there were any errors
       
    if(count($errors) == 0) {
          
    // finish processing form, then redirect to success page:
          
    header('Location: http://yoursite.com/order/success.php');
          exit;
       }
    }
    ?>
    <html><head><title>Test</title></head><body>
    <?php
    if(count($errors)) {
       foreach(
    $errors as $err) {
          echo 
    "<p class='error'>".htmlspecialchars($err)."</p>\n";
       }
    }
    ?>
    <form method='post' action=''>
    <!-- form fields here -->
    </form>
    </body></html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Now trying to master the single page form processor arrangement. It works until I begin to introduce the captcha! (developed from a randomimage script which combines a background image with text) The script installed into the two page form fairly easily.
    <hr>

    I wish to install and display the following type of error message into the one page processor/form below:

    Code:
    if (md5($captcha) == $_SESSION['image_random_value']) 
            {/*  echo "<h2>Code $captcha valid </h2>\n"; */}
    	       else
    	       echo "Link cannot be sent because you entered an invalid Code. Please validate again!\n";
    I am not having any luck with the installation! The first array of error messages (name, email & captcha) work fine, but the captcha processing is snagging and preventing the call or display of the thanku.html.The code is as follows and I have added color to some of the components of the code to facilitate.

    Can I be put back on the right track ?




    Code:
    <?php
    session_start();
    /////////// LINK FORM AND PROCCESOR (All on one page) VALIDATION STARTS HERE ////////////////////	
    	$errors = array();
    	function validate_form_items()
    	{
    	$form_items = array(		   
    	"name"  => array(
    		"regex" => "/^([a-zA-Z '-]+)$/",
    		"error" => "<i> Appears in an improper format!</i>",),
    	"email" => array(
    		"regex" =>
    		"/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/",
    		"error" => "<i> Enter a valid Email address!</i>",),
    	"captcha" => array(
    		"regex" =>  "/md5$captcha/",
    		"error" => "<i> Please enter a Validate Code!</i>",),);		
    	global $errors;		
    		if(!preg_match($form_items["name"]["regex"], $_POST["m_name"]))
    			$errors[] = "<b>Your Name:</b> ".$form_items["name"]["error"];
    		if(!preg_match($form_items["email"]["regex"], $_POST["m_email"]))
    			$errors[] = "<b>Your Email:</b> ".$form_items["email"]["error"];		
    		if(!preg_match($form_items["captcha"]["regex"], $_POST["captcha"]))
    			$errors[] = "<b>Validate Code:</b> ".$form_items["captcha"]["error"];
    				return count($errors);	   
    	 }
    /////////FORM MAIL STARTS HERE//////////////////////////////////////////////////////////////////////////////////	
    	function email($from, $from_name, $to, $message)
    	{
    		//header("Location: thanku.html");return;
    
    		$headers .= "From: ".$from."\r\n";
    		$headers .= "Content-type: text/plain; charset=ISO-8859-1";		
    		$domian_name = "www.domain.net";
    		$subject = $from_name."- An invitation to view $domian_name";
    		$your_message = "Hi!\r\n";
    		$your_message.= ucfirst($from_name);
    		$your_message.= "would like you to check out this website at $domian_name\r\n";
    		$message=$your_message.stripslashes($message);
    		if (mail($to,$subject,$message,$headers) ) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	function print_error($errors)
    	{foreach($errors as $error)
    		{$err.=$error."<br/>";}
    		echo "<div style=\"font-size:14px; font-weight:normal; color:red;\"><span style=\"background-color:yellow\">$err</span><div>";		
    	}
    //////////////POSTING AND THANK YOU STARTS HERE///////////////////////////////////////////////////////////////////////////	
    	function form_process()
    	{	
           $captcha = $_POST['captcha'];//validate code		             $from_name = $_POST["m_name"];
    		$fromm_email = $_POST["m_email"];
    		$to = $_POST["f_email"].",".$_POST["f_email"];
    		$message = $_POST["message"];		
    		$error_count = validate_form_items();
    		if($error_count == 0)		
    		{if(email($fromm_email, $from_name, $to,)) header("Location: thanku.html");
    			else
    		{global $errors; $errors[] = "Email could not be send at this time. <br />Please report this to the Webmaster.";} }		
    	}
    		if(isset($_POST["submit"])) form_process();
    ?>
    
    <html><body><fieldset><legend>Link to my Friend/s.</legend><br />
    Install your friend/'s email details to forward link to this website.<br /><br />
    <i>Required *</i>
        <form id="link" method="post" action="<?php echo $PHP_SELF?>" >
        <div><?php global $errors; if(count($errors) != 0){ print_error($errors);} ?></div>	
    	<label>My Name:*</label><input type="text" name="m_name" value="<?php echo $_POST["m_name"]?>" />
        <label>My Email:*</label><input type="text" name="m_email" value="<?php echo $_POST["m_email"]?>" />
        <label>Friend's Email:*</label><input type="text" name="f_email" value="<?php echo $_POST["f_email"]?>" />
    
    	<img src="randomImage.php"/><input type="text" name="captcha" size="5" />	
        <input type="submit" name="submit" value="Send Website Link" />
    </form></fieldset></body></html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles