2017-04-01 38 views
0

我在php中創建自定義表單並使用數據庫。我想將這種形式整合到wordpress中;爲此我創建一個模板(customform.php)並添加到頁面中。我的php代碼可以在xampp上運行,但不能在wordpress中運行。數據庫嵌入在wordpress中的PHP表單

請幫我把代碼整合到wordpress中。

/*CustomForm php*/ 
<?php /* Template Name: CustomForm */ ?> 
<?php require_once("conn.php");?> 

<?php get_header(); ?> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 

<div id="content" class="full-width"> 
    <?php while (have_posts()) : the_post(); ?> 
     <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 

<body> 
    <form name="course_information" id="course_information" method="post" action="course_information_action.php" > 
    <div> 
    <h1 class="heading">Product Details</h1> 
    <h3>Product Type</h3> 
    <?php 
     $sql = "select id,course from course_master"; 
     $result = mysqli_query($con,$sql); 
     $selectbox = ''; 

     $selectbox .= '<select id="course_master" name="course_master" class="select_style"><option value="0"> -- select --</option>'; 
     while($data = mysqli_fetch_assoc($result)) 
     { 
      $selectbox .= "<option value=".$data['id'].">".$data['course']."</option>"; 
     } 
     $selectbox .= '</select>'; 
     echo $selectbox; 
    ?> 

    <h3>Product</h3> 

    <div><select class="select_style" id="course_details" name="course_details" > 
     <option value="0"> -- select --</option> 
    </select></div> 


    <p></p> 
    <div id="ex_data"> 
    <input type="checkbox" name="check" id="check" value="" /> I need a product demonstration, installation assistance and for other queries at no additional cost</div> 

    <p></p> 
    <div id="after_check"> 
    <div><textarea rows="5" cols="50" name="requirements" id="requirements" placeholder="Please provide your requirement here"></textarea></div> 

    <h3>Address</h3> 
    <div><textarea rows="5" cols="50" name="address" id="address" placeholder="Please provide your address for visit purpose"></textarea></div> 
    </div> 
    <span id="key_val"> 
    <h3>Please enter existing serial for extend subscription: </h3> 
    <div><input type="text" name="key" id="key" value="" /></div> 
    </span> 


    <h3>No. of licenses<span class="star_require">*</span> : </h3> 
    <div><input type="text" name="licenses" id="licenses" value="" required /></div> 

    <h3>Sub Total : </h3> 
    <span id="sub_total"></span> 
    <div><input type="hidden" name="price" id="hidden_subtotal" value="" /></div> 

    <div><input class="form_btn" type="submit" id="continue_first" value="continue" /></div> 
    </div> 

    <div id="contact_details"> 
    h1 class="heading">Contact Details</h1> 
    <h3>Email Address<span class="star_require">*</span>:</h3> 
    <div><input type="text" id="email" name="email" value="" required /></div> 

    <h3>Mobile <span class="star_require">*</span>: </h3> 
    <div><input type="text" id="mobile" name="mobile" value="" maxlength="10" required /></div> 


    <div><input class="form_btn" type="submit" id="continue" value="continue" /></div> 
    </div> 

    <div id="billing_details"> 
    h1 class="heading">Billing Details</h1> 
    <h3>Your Name/Organisation Name<span class="star_require">*</span>:</h3> 
    <div><input type="text" id="name" name="name" value="" required /></div> 

    <h3>Your Area PIN code <span class="star_require">*</span>: </h3> 
    <div><input type="text" id="pincode" name="pincode" value="" maxlength="10" required /></div> 


    <div><input class="form_btn" type="submit" id="submit" value="submit" /></div> 

    </div> 
</form>   


     </div> 
    <?php endwhile; ?> 
</div> 
<?php get_footer(); 


/*contact form.js*/ 

// JavaScript Document 
$(document).ready(function(){ 
     $("#key_val").hide(); 
     $("div#ex_data").hide(); 
     $("div#after_check").hide(); 
     $no = $("#licenses").val(); 

     $("#licenses").keypress(function (e) { 
      //if the letter is not digit then display error and don't type anything 
      if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {    
         return false; 
      } 
      }); 


     $("#course_master").change(function(){ 
      var cid = $(this).val();    
      if(cid == 2) 
      { 
       $("#key_val").show(); 
       $('#licenses').attr('disabled', 'disabled'); 
       $("div#ex_data").css("display","none"); 
      } 
      else{ 
       $("#key_val").hide(); 
       $("div#ex_data").show(1000); 
       $("#licenses").removeAttr("disabled"); 
      } 
      $.ajax({ 
       type:"POST", 
       url:"get_course_details.php", 
       data:"cid="+cid, 
       success:function(msg) 
       {     
        $("#course_details").html(msg); 
       } 
      }); 

     }); 

     $("#course_details").change(function(){ 
      var id = $(this).val(); 
      $.ajax({ 
       type:"POST", 
       url:"get_course_info.php", 
       data:"id="+id, 
       success:function(msg) 
       {     
        $("#hidden_subtotal").val(msg); 
        if($no == '') 
        { 
         var total = msg * 1; 
         $("#sub_total").text(total); 
        } 
        else 
        { 
         var total = msg * $no; 
         $("#sub_total").text(total); 
        } 
       } 
      });    
     }); 

     $("#check").click(function(){ 
      if($(this).is(":checked")){ 
       $("div#after_check").show(1000); 
       $("div#after_check #address").attr("required","true"); 
       $("div#after_check h3").append("<span class='star_require'>*</span>"); 
      } 
      else 
      { 
       $("div#after_check").hide(1000); 
       $("div#after_check #address").attr("required","false"); 
      } 
     }); 



     $("#key").blur(function(){   
      var key = $("#key").val(); 
      $.ajax({ 
       type:"POST", 
       url:"validate_key.php", 
       data:"key="+key, 
       success:function(msg) 
       {     
        if(msg ==1) 
        { 
         $("#licenses").removeAttr("disabled"); 
        } 
        else 
        { 
         $('#licenses').attr('disabled', 'disabled'); 
        } 
       } 
      }); 

     }); 

     $("#licenses").blur(function(){   
      var subtotal = $("#hidden_subtotal").val(); 
      if(!isNaN(subtotal) && subtotal != '' && subtotal != 0) 
      { 
       subtotal = subtotal * $("#licenses").val(); 
       $("#sub_total").text(subtotal); 
      } 

     }); 

     $("#continue_first").click(function(){ 
      $("div#contact_details").css("display","block"); 

     }); 
     $("#continue").click(function(){ 
      $("div#billing_details").css("display","block"); 

     }); 

     $("#submit").click(function(){ 
      var email = $("#email").val(); 
      var mobile = $("#mobile").val(); 
      var pincode = $("#pincode").val(); 
      if(!isValidEmailAddress(email)) { 
       alert("invalid email address!"); return false; 
      } 
      else if(isNaN(mobile) || (mobile.length < 10)) 
      { 
       alert("invalid mobile no!"); return false; 
      } 
      else if(isNaN(pincode)) 
      { 
       alert("Invalid Pincode!");return false; 
      } 
      else{ 
       alert("success"); 
       $("#course_information").submit(); 
       return true; 
      } 

     }); 

    }); 



    //if(!isValidEmailAddress(emailaddress)) { /* do stuff here */ } 

    function isValidEmailAddress(emailAddress) { 
    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i; 
    return pattern.test(emailAddress); 
}; 

回答

0

首先在wordpress中進行集成,你不需要創建配置文件和包含。只需在您使用的主題中創建模板,然後在wordpress admin中創建頁面並調用此模板。

相關問題