0

Am trying to call PHP validation file into a HTML file. Could any one suggest me how to call that php file when click on submit button.

Sample html code:

<head>
    <style>
        .error {color: #FF0000;}
    </style>
</head>
<body style="background-color:#F2F2F2">
    <form name="htmlform" method="post" onsubmit="\test_form.php">
        <table width="550px" table align="center" frame="box" height="80%" style="margin-top:25px;margin-bottom:25px; background-color:#ffffff;border-radius:5px;"></tr>
            <tr>
                <th colspan="2">
                    <h3  align="center">Write to Us</h3>
                    <hr>
                </th>
            </tr>
            <tr>
                <td valign="top">
                    <label for="name">Name </label>
                </td>
                <td valign="top">
                    <input  type="text" name="name" maxlength="50" size="30">
                    <span class="error">* <?php echo $nameErr;?></span>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="email">Email Address </label>
                </td>
                <td valign="top">
                    <input  type="text" name="email" maxlength="80" size="30">
                    <span class="error">* <?php echo $emailErr;?></span>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="telephone">Telephone Number</label>
                </td>
                <td valign="top">
                    <input  type="text" name="telephone1" maxlength="30" size="30">
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="message">Message </label>
                </td>
                <td valign="top">
                    <textarea  name="message1" maxlength="1000" cols="25" rows="6"></textarea>
                    <span class="error">* <?php echo $messageErr;?></span>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center;">
                    <input type="submit" value="Submit" onclick="$.fancybox.close()">
                </td>
            </tr>
        </table>
    </form>
</body>

and PHP code is as follows:

<?php
    if (isset($_POST['submit']))
    {
        if (empty($_POST["name"]))
            {$nameErr = "Name is required";}
        else
            {$name =test_input($_POST["name"]);}

        if (empty($_POST["email"]))
            {$emailErr = "Email is required";}
        else
            {$email = test_input($_POST["email"]);}

        if (empty($_POST["telephone1"]))
            {$telephone1 = "";}
        else
            {$telephone1 =test_input($_POST["telephone1"]);}

        if (empty($_POST["message1"]))
            {$messageErr = "Message is required";}
        else
            {$message1 = test_input($_POST["message1"]);}

        if(isset($name) && isset($email) && isset($message1))
        {
            echo "<h2>Your Input:</h2>";
            echo $name;
            echo "<br>";
            echo $email;
            echo "<br>";
            echo $telephone1;
            echo "<br>";
            echo $message1;
            $recipient = "[email protected]"; ///  Your Email address
            if (isset($_POST['email']))
            {
            //Send Mail To Webmaster
                $email = $_POST['email'] ;
                $subject = 'Feedback Form';
                $message =  $name . ' has been subscribed to your website.';
                mail("$recipient", $subject, $message, "From:" . $recipient);
            }
        }
    }
    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>

It is not accepting when I keep php code into html file and save it as php and execute in word press.

2
  • 1
    Where is the test_form.php, is it in the same directory as html? Commented Mar 4, 2014 at 12:03
  • yes it is in the same directory but not performing any validation Commented Mar 4, 2014 at 12:28

2 Answers 2

2
<form method="post" onsubmit="\test_form.php">

change it to:

<form method="post" action="test_form.php">
Sign up to request clarification or add additional context in comments.

3 Comments

it is moved to test_form.php file and not displaying anything just blank page and no validations. Please suggest some solution as such it check validations for the fields specified
@Pallavipradeep all the files are stored in same directory ?
files are saved in same directory but it just displays that page without validating
0

what is the path of the first and second page ?

use this instead : http://jqueryvalidation.org/documentation/

$().ready(function() {
// validate form on keyup and submit
$("#htmlform").validate({
    rules: {
        name: {
            required: true,

        },
        email: {
            required: true,
            email:true
        }....

}
});

For WP take a look to this , it's might be usefull , you don't need to code a lot , just config in WP

6 Comments

Any purpose for that ? If you want use just php , try to check your directories remember that ./ go one dir ago , and ../ to the root
i would like to keep this html page in wordpress and it is taking only html file and PHP is taken as text and not accepting. It should work from backend only that it can be called.
You can activate jQuery on WP with <?php wp_enqueue_script("jquery"); ?>
Azrael can i have piece of code which calls that particular code with HTML and jQuery code. Thanks in Advance
with jQuery validations i have done form validations by using this link runnable.com/Uaki4q9qiqlLAACJ/… and removed php code in it and asked to navigate to another page it is working fine. But would like to keep this code in WP and check. Thanks for suggestion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.