dqvrlgi3247 2013-12-01 14:33
浏览 56
已采纳

在HTML中调用/使用PHP函数

I am practicing with PHP and trying to build Validation for my HTML register form, My idea is to create PHP function for each input field within my HTML form and call it if the field has been left out empty.

Am at a stage where everything is ready but I dont know how to call my functions within HTML...

Could some one sugest a way of calling a functions withing HTML form.

session_start();

 if(isset($_POST['reg'])){

 $lastname = mysql_real_escape_string($_POST['lastname']);
 $email = mysql_real_escape_string($_POST['email']);
 $tel = mysql_real_escape_string($_POST['telephone']);
 $firstname = mysql_real_escape_string($_POST['firstname']);

 }
 class Validation{

    public function FirstName(){
        if($firstname == NULL){
            echo '<p>Enter Name</p>';
        }   
    }

    public function LastName(){
        if($lastname == NULL){
            echo '<p>Enter \surname</p>';
        }
    }

    public function EmailAdress(){
        if ($email == NULL){
            echo '<p>Enter Email</p>';
        }
    }

    public function TelephoneNumber(){
        if($tel == NULL){
            echo '<p>Enter Name</p>';
        }
    }
}

 ?>

<!-- banner -->
<div class="hero-unit banner-back">
    <div class="container">
        <div class="row-fluid">
            <div class="span8">

            </div>

            <div class="span4 form-back text-center">
                <form name="reg" action="" method="post">
                    <fieldset>
                        <legend>Free Workshop</legend>
                        <div class="input-wrapper">
                            <input type="text" placeholder="First Name" id="firstname" name="firstname <? FirstName(); ?>"/>
                        </div>
                        <div class="input-wrapper">
                            <input type="text" placeholder="Last Name" id="lastname" name="lastname"/>
                        </div>
                        <div class="input-wrapper">
                            <input type="email" placeholder="Email Address" id="email" name="email"/>
                        </div>
                        <div class="input-wrapper telephone">
                            <input type="text" placeholder="Telephone number" id="telephone" name="telephone"/>
                        </div>
                        <div class="input-wrapper">

展开全部

  • 写回答

1条回答 默认 最新

  • dp518158 2013-12-01 14:40
    关注

    You cannot call PHP from HTML after you delivered the Website to the Browser. (well you can with AJAX, but that would be some overkill). When the user does some input to the fields the page is not sent to your server (where PHP is interpreted).

    So I suggest you do the validation client side in JavaScript.

    To output PHP Variables or the return of a Function call simply use the <?= myFunction() ?> syntax.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部