dqvrlgi3247 2013-12-01 22:33
浏览 54
已采纳

在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 22: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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器