• Resolved margonline

    (@margonline)


    I’ve created a template page to enable an admin user to enter some fields into a custom table I’ve created as part of the wordpress d/b. The page displays as it should but when the form is submitted, the index.php page with all the posts is displayed. How do I get the form to be submitted back to the template page to perform the validation and then display an error message or do the table insert. I would be grateful for some advice / pointers. Thanks! code below

    <?php
    /*
    Template Name: Postcode Form
     */
    
    get_header();
    if ($_SERVER['REQUEST_METHOD']=='post') {
    	//validate form
    	$em=$pc=false;
    	$pc = (isset($_POST['pc']) && strlen($_POST['pc']) == 2) ? filter_var($_POST['pc'],FILTER_SANITIZE_STRING) : false;
    	$em = (isset($_POST['email']) ) ? filter_var($_POST['email'],FILTER_SANITIZE_EMAIL) : false;
    	if (!$em && !$pc) {
    	//insert to d/b
    		//$wpdb
    		echo '<div>No errors</div>';
    	}
    	else {
    		echo '<div>Please enter a valid postcode prefix and a valid email address</div>';
    	}
    }
    ?>
    
    <form id="emails" action="postcode_form.php" method="post">
    <div>Enter postcode prefix(first 2 characters)
    <input type="text" name="pc"/></div>
    <div>Enter  email address
    <input type="text" name="email" /></div>
    <div><input type="submit" name="submit" value="Submit" />
    </form>
    
    <?php
    get_footer();
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • First thing I would try is change
    <form id="emails" action="postcode_form.php" method="post">
    to
    <form id="emails" action="" method="post">

    That will at least send the info back to itself. Nest step will be the harder task of inserting the info into the tables. I’ll leave that into someone else’s capable mind.

    Thread Starter margonline

    (@margonline)

    Thanks bythegram, at least now the page is redisplaying itself when submit is pressed but the messages are not being displayed. I’ve changed echo to print and print_r to no avail. Any suggestions?

    Also where would I have learned that you need to set action to “” to get it to send the form back to itself?

    Thanks again for your help

    Basically action=”” is the url to send the form information w3schools
    This way if you wanted to have the success message on a separate page you could do so by changing the action to

    action=”pageaddress”

    As far as it not working yet, do you have a link you can provide?

    Thread Starter margonline

    (@margonline)

    Yeah I understand what the action is for. I want the success message or the error message on the same page, so the admin user can make his corrections or if correct, enter another set of data. At the moment I’m not even attempting the insert to the table, just trying to get all the files to talk to each other.

    Working on localhost at the moment but will try to set up something that I can point you to. thanks again for any suggestions!

    Thread Starter margonline

    (@margonline)

    got this working – there was an error in my code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘form validation, integrating into WordPress’ is closed to new replies.