如何使用php为html标签和数据库连接添加IF条件

时间:2016-01-29 05:44:52

标签: php html mysql

嗨我正在联系我们表格,向多个用户发送电子邮件,我需要在HTML和数据库文件中添加条件,因为如果我选择下拉列表,则必须更改列。但是如果我添加IF条件也显示所有。这是我的代码。

contact.php

           <form method= "post" action="contactus.php" id="myform">                
                <p>
                <label for="user">User</label>
                <select id="user" name="user">
                                <option value="employee">Employee</option>
                                <option value="company">Company</option>
                                <option value="freelancer">Freelancer</option>
                </select>
                </p> 

            <?php if('selected==company'):?>
                <p>
                <label for="username">Your Name *</label>

                <input id="username" type="text" name="username" value="" placeholder="Enter your name here" required />

                </p>

                <p>

                <label for="email">Email *</label>

                <input id="email" type="email" name="email" value="" placeholder="Enter E-mail id here" required/>

                </p>

                 <?php endif;?>              

                <p>

                <label for="mobile_no">Phone</label>

                <input id="mobile_no" type="text" name="mobile_no"  placeholder="Enter mobile no. here" />

                </p>

                <p>

                <label for="subject">Subject</label>

                <input id="subject" type="text" name="subject" value=""  />

                </p>    

                <p>

                <label for="message">Message</label>

                <textarea id="message"  name="message"   ></textarea>

                </p>

                <p>

               <button id="submit" type="submit" name="submit">Submit</button>

                </p>

Contactus.php

<?php
    $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
    $db = mysql_select_db("accountant", $connection);
    $uname = $_POST['username'];
    $email = $_POST['email'];   
    $phone = $_POST['mobile_no'];   
    $message = $_POST['message'];       
    $subject=$_POST['subject'];         
    $sql2 = mysql_query("insert into contact_us(username,email,mobile_no,message,subject) values ('$uname','$email','$phone','$message','$subject')");
    if ($sql2) 
    {
      $to = "email@gmail.com,mail@gmail.com";
         $subject = $_POST['subject'];        
         $message = $_POST['message'];       
         $retval = mail ($to,$subject,$message);    
    echo "Message sent successfully...";
         }else {
            echo "Message could not be sent...";
         }
?>

任何人都可以帮我如何在contact.php和contactus.php中添加条件,数据应该插入到database.it对我来说非常需要。

3 个答案:

答案 0 :(得分:0)

您应该使用javascript来实现此功能您需要编写如下代码: -

<强> contact.php

 <form method= "post" action="contactus.php" id="myform">
            <p>
            <label for="user">User</label>
            <!-- Add onchange function myFunction() here -->
            <select id="userd" name="user" onchange="myFunction()">
                         <option value="">Select</option>
                        <option value="employee">Employee</option>
                        <option value="company">Company</option>
                        <option value="freelancer">Freelancer</option>
            </select>
            </p>


        <!-- Add div here. Show/Hide it based on selected option using javascript -->
        <div id='optional'>
            <p>
            <label for="username">Your Name *</label>
            <input id="username" type="text" name="username" value="" placeholder="Enter your name here" required />
            </p>
            <p>
            <label for="email">Email *</label>
            <input id="email" type="email" name="email" value="" placeholder="Enter E-mail id here" required/>
            </p>
        </div>  
        <!-- End of the div -->


            <p>
            <label for="mobile_no">Phone</label>
            <input id="mobile_no" type="text" name="mobile_no"  placeholder="Enter mobile no. here" />
            </p>
            <p>
            <label for="subject">Subject</label>
            <input id="subject" type="text" name="subject" value=""  />
            </p>  

            <p>
            <label for="message">Message</label>
            <textarea id="message"  name="message"   ></textarea>
            </p>

            <p>
             <button id="submit" type="submit" name="submit">Submit</button>
            </p>

    </form>  

将此Js添加到</body>标记上方的此文件中,如下所示: -

<script type="text/javascript">
   function myFunction(){
        var e = document.getElementById("userd");
        var company = e.options[e.selectedIndex].value;
            if(company != 'company'){
            document.getElementById('optional').style.display = 'none';
           }else{
            document.getElementById('optional').style.display = 'block';
           }            
    }          
</script> 

您的联系人文件应为: -

<强> Contactus.php

<?php
    $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
    $db = mysql_select_db("accountant", $connection);
    // check username is exist or not in $_POST
    $uname = isset($_POST['username']) ? $_POST['username'] : '';
    // check email is exist or not in $_POST
    $email = isset($_POST['email']) ? $_POST['email'] : '';   
    $phone = $_POST['mobile_no'];   
    $message = $_POST['message'];       
    $subject=$_POST['subject'];  
    // check $uname and $email is set or not 
    if(empty($uname) && empty($email)){
        $sql2 = mysql_query("insert into contact_us(mobile_no,message,subject) values ('$phone','$message','$subject')");
    }else{
       $sql2 = mysql_query("insert into contact_us(username,email,mobile_no,message,subject) values ('$uname','$email','$phone','$message','$subject')");
    }

    if ($sql2) 
    {
         $to = "email@gmail.com,mail@gmail.com";
         $subject = $_POST['subject'];        
         $message = $_POST['message'];       
         $retval = mail ($to,$subject,$message);    
    echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
?>

希望它会对你有所帮助:)。

答案 1 :(得分:0)

您应该使用javascript或jquery来实现此功能。我使用过jquery。请更新您的代码,如下所示,并检查:

contact.php

<form method="post" action="contactus.php" id="myform">


<p>
        <label for="user">User</label> <select id="user" name="user" onchange="showRequiredOption(this.value);">
            <option value="employee">Employee</option>
            <option value="company">Company</option>
            <option value="freelancer">Freelancer</option>
        </select>
    </p>

    <p style="display:none;" id="showName">

        <label for="username">Your Name *</label> <input id="username"
            type="text" name="username" value=""
            placeholder="Enter your name here" />
    </p>
    <p style="display:none;" id="showEmail">

        <label for="email">Email *</label> <input id="email" type="email"
            name="email" value="" placeholder="Enter E-mail id here" />
    </p>

    <p>

        <label for="mobile_no">Phone</label> <input id="mobile_no" type="text"
            name="mobile_no" placeholder="Enter mobile no. here" />
    </p>

    <p>

        <label for="subject">Subject</label> <input id="subject" type="text"
            name="subject" value="" />
    </p>

    <p>
        <label for="message">Message</label>
        <textarea id="message" name="message"></textarea>
    </p>

    <p>
        <button id="submit" type="submit" name="submit">Submit</button>
    </p>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function showRequiredOption(cval)
{
    if(cval=='company')
    {
        $('#showName').show();
        $('#showEmail').show();
        $('#username').attr('required','required');
        $('#email').attr('required','required');
    }
    else
    {
        $('#showName').hide();
        $('#showEmail').hide();
        $('#username').removeAttr('required');
        $('#email').removeAttr('required');
    }
}
</script>

Contactus.php

<?php
    $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
    $db = mysql_select_db("accountant", $connection);
    $uname = isset($_POST['username']) ? $_POST['username'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';   
    $phone = $_POST['mobile_no'];   
    $message = $_POST['message'];       
    $subject=$_POST['subject'];  
    if(empty($uname) && empty($email)){
        $sql2 = mysql_query("insert into contact_us(mobile_no,message,subject) values ('$phone','$message','$subject')");
    }else{
       $sql2 = mysql_query("insert into contact_us(username,email,mobile_no,message,subject) values ('$uname','$email','$phone','$message','$subject')");
    }

    if ($sql2) 
    {
         $to = "email@gmail.com,mail@gmail.com";
         $subject = $_POST['subject'];        
         $message = $_POST['message'];       
         $retval = mail ($to,$subject,$message);    
    echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
?>

答案 2 :(得分:0)

。 PHP是服务器端语言,因此它将呈现所有内容并将其传递给客户端,因此如果您需要使用php执行某些操作,那么您需要传递给服务器然后执行。 如果你想在客户端执行某些东西你需要使用JS或JQuery或任何其他客户端语言。

JS FIDDILE EXAMPLE of yours

<form method= "post" action="contactus.php" id="myform">

            <p>

            <label for="user">User</label>

            <select id="user" name="user">
                            <option value="employee">Employee</option>
                            <option value="company">Company</option>
                            <option value="freelancer">Freelancer</option>
                            </select>

            </p>

<div id='username'>
            <p>

            <label for="username">Your Name *</label>

            <input id="username" type="text" name="username" value="" placeholder="Enter your name here" required />

            </p>

            <p>

            <label for="email">Email *</label>

            <input id="email" type="email" name="email" value="" placeholder="Enter E-mail id here" required/>

            </p>
            </div>
            <p>

            <label for="mobile_no">Phone</label>

            <input id="mobile_no" type="text" name="mobile_no"  placeholder="Enter mobile no. here" />

            </p>

            <p>

            <label for="subject">Subject</label>

            <input id="subject" type="text" name="subject" value=""  />

            </p>    

            <p>

            <label for="message">Message</label>

            <textarea id="message"  name="message"   ></textarea>

            </p>

            <p>

           <button id="submit" type="submit" name="submit">Submit</button>

            </p>
<script>
        $('select').on('change', function() {
      if(this.value == "company"){
      $("#username").show();
      }else{
      $("#username").hide();
      }
      });
</script>