-3

[registervistor.html]

<p>
<b>Userid:</b>  <input type="text" name="userid" size="20" maxlength="80" value="" /><br>
</p>

<p>
<b>Password:</b>  <input type="password" name="password" size="20" maxlength="80" value=""  /><br>
</p>

<p>
<b>Name:</b>  <input type="text" name="name" size="20" maxlength="40" value=""  /><br>
</p>

<p>
<b>Gender:</b>  
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
</p>

<p>
<!-- Write codes to add in the dropdown lists for birthday -->
<b>Birthday:</b>
<select name="Day">
 <option selected="">Please Select</option>
 <option value="1">01</option>
 <option value="2">02</option>
 <option value="3">03</option>
 <option value="4">04</option>
 <option value="5">05</option>
 <option value="6">06</option>
 <option value="7">07</option>
 <option value="8">08</option>
 <option value="9">09</option>
 <option value="10">10</option>
 <option value="11">11</option>
 <option value="12">12</option>
 <option value="13">13</option>
 <option value="14">14</option>
 <option value="15">15</option>
 <option value="16">16</option>
 <option value="17">17</option>
 <option value="18">18</option>
 <option value="19">19</option>
 <option value="20">20</option>
 <option value="21">21</option>
 <option value="22">22</option>
 <option value="23">23</option>
 <option value="24">24</option>
 <option value="25">25</option>
 <option value="26">26</option>
 <option value="27">27</option>
 <option value="28">28</option>
 <option value="29">29</option>
 <option value="30">30</option>
 <option value="31">31</option>
</select>

<select name="Month">
 <option selected="">Please Select</option>
 <option value="January">January</option>
 <option value="February">February</option>
 <option value="March">March</option>
 <option value="April">April</option>
 <option value="May">May</option>
 <option value="June">June</option>
 <option value="July">July</option>
 <option value="August">August</option>
 <option value="September">September</option>
 <option value="October">October</option>
 <option value="November">November</option>
 <option value="December">December</option>
</select>

<input type="number" min="1960" max="2014" name="Year" size="4" maxlength="4" value="Year" />

</p>

<p>
<b>Categories:</b>  
<input type="checkbox" name="categories[]" value="Appetising Starters!">Appetising Starter
<input type="checkbox" name="categories[]" value="Main Courses!">Main Course
<input type="checkbox" name="categories[]" value="Refreshing Drink!">Refreshing Drink
<input type="checkbox" name="categories[]" value="Yummy Desserts!">Yummy Dessert
</p>

I want to display error messages on the same page as the form that the user use to enter his/her data using PHP Redux but I don't understand how to. I want these codes to appear in registervistor.html instead of vistorprofile.php . Can someone help me for this?

        <?php   
        if (!empty($_POST['name'])){
            $name = $_POST['name'];
        }
        else {
            $name = NULL;
            echo "<p> You forgot to enter your name! </p>";
        }

        if (!empty($_POST['userid'])) {
            $userid=$_POST['userid'];
        }
        else {
            $userid = NULL;
            echo "<p> You forgot to enter your user ID! </p>";
        }

        if (!empty($_POST['password'])){
            $password = $_POST['password'];
        }
        else {
            $password = NULL;
            echo "<p> You forgot to enter your password! </p>";
        }

        if (!empty($_POST['gender'])){
            $gender = $_POST['gender'];

        }
        else {
            $gender = NULL;
            echo "<p> You forgot to enter your gender! </p>";
        }
        // A null in birth date keeps appearing even if the details are key in //
        if (!empty($_POST['Day'])){
            $Day = $_POST['Day'];
        }

        else {
            $Day = NULL;
            echo "<p> You forgot to enter your birth date! </p>";

        }

        if (!empty($_POST['Month'])){
            $Month = $_POST['Month'];
        }

        else {
            $Month = NULL;
            echo "<p> You forgot to enter your birth month! </p>";

        }   

        if (is_numeric($_POST['Year'])){
            $Year = $_POST['Year'];

        }

        else {
            $Year = NULL;
            echo "<p> You forgot to enter your birth year! </p>";

        }

        if (isset($_POST['categories'])){
            $categories = $_POST['categories'];
        }
        else {
            $categories = NULL;
            echo "<p> You forgot to indicate your categories! </p>";
        }

        if ($userid && $password && $name && $gender && $Day && $Month && $Year && $categories) {
            echo "<b><i>$name</i></b>, thank you for registering with us! <br>";    
            echo "You have entered the following details: <br>";
            echo "UserID: $userid <br>";
            echo "Gender: $gender <br>";
            echo "Birthday: $Day / $Month / $Year <br>";
            echo "Categories: ";
            foreach ($categories as $value){
                echo "$value <br>";
            }
            echo "<p>Please <a href='recipesummary.php'>Click Here</a> to continue viewing the recipes! </p>";
        }   

        else {
            echo "<p> Please <a href='registervisitor.html'>Click Here</a> to go back to fill in the form again! </p>";
        }

        echo "<br> <br>";   ?>
SKYF
  • 11
  • 1
  • 4

1 Answers1

1

i would suggest you to use jquery form validation:
http://jqueryvalidation.org/documentation/

to run php content inside html file, details:
How to run a php script inside a html file?

But you can use jquery/ajax form submission, form at .html file and all validation at php file:
jQuery Ajax POST example with PHP

HTML

<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>

Javascript

// Variable to hold request
var request;

// Bind to the submit event of our form
$("#foo").submit(function(event){

    // Abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);

    // Let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");

    // Serialize the data in the form
    var serializedData = $form.serialize();

    // Let's disable the inputs for the duration of the Ajax request.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

    // Prevent default posting of form
    event.preventDefault();
});

in PHP file

// You can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$bar = $_POST['bar'];

(copy paste from last link)

hope this help.

Community
  • 1
  • 1
crazymoin
  • 336
  • 3
  • 13
  • I'm not allow to use jquery for this.. Thank you anyway(: – SKYF Jan 01 '15 at 10:14
  • @SKYF you can't use php inside a .html file (registervistor.html). you can use a html file as php if you change htaccess file (check the 2nd link). – crazymoin Jan 01 '15 at 10:38