hi iam doing contact form sending emails multiple users in need add if condition in html in database files, because if select dropdown according columns has changed.but if add if condition displaying all.here code.
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 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 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 not sent..."; } ?>
can me how add condition in both contact.php , contactus.php , data should inserted database.it needfull me.
you should use javascript or jquery functionality. have used jquery
. please update code below , check :
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 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 contact_us(mobile_no,message,subject) values ('$phone','$message','$subject')"); }else{ $sql2 = mysql_query("insert 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 not sent..."; } ?>
Comments
Post a Comment