Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

php - Insert Dynamic Select Box Value In Mysql Database And Show Data Submitted Message

In the below form i am loading question and answers from mysql database based on asset title value and submitting dynamic custom questions value to database. ( this is working fine ) but my problem is i am not getting message when form value is submited to database for assets who have less than 15 custom questions.

I guess i am doing something wrong in my createlead.php file if condition...

Please help.

If you think this problem can be solved with another method then please let me know.

enter image description here enter image description here Loading dynamic custom questions from mysql database based on asset title value

<?php  
 //load_data.php  
 $connect = mysqli_connect("localhost", "root", "", "test");  
 $output = '';
 if(isset($_POST["assetid"]))  
 {  
  if($_POST["assetid"] != '')
  {  
       $sql = "SELECT * FROM cq WHERE assetid = '".$_POST["assetid"]."'";

  }  
  else
  {  
       $sql = "SELECT * FROM cq WHERE assetid = 'akash'";
       // i dont want to load any database so used wrong query

  }
  $result = mysqli_query($connect, $sql);
  $quesno = 1;

  while($row = mysqli_fetch_array($result))  
  {  
       $output .= '<div class="col-sm-6">';
       $output .= '<div class="form-group">';
       $output .= '<label for="campaignname">'.$row["cqname"].'</label>';
       $output .= '<select name="cq'.$quesno.'" size="5" class="form-control" required>';
       // $output .= '<div style="border:1px solid #ccc; padding:20px; margin-bottom:20px;">'.$row["product_name"].'</div>';
       // $output .= '<tr><td>'.$row["cqname"].'<br></td>';
       $output .= '<option value="" required>Select Custom Question Answer</option>
                   <option value="'.$row["cqa1"].'">'.$row["cqa1"].'</option>
                   <option value="'.$row["cqa2"].'">'.$row["cqa2"].'</option>
                    <option value="'.$row["cqa3"].'">'.$row["cqa3"].'</option>
                    <option value="'.$row["cqa4"].'">'.$row["cqa4"].'</option>
                    <option value="'.$row["cqa5"].'">'.$row["cqa5"].'</option>
                    <option value="'.$row["cqa6"].'">'.$row["cqa6"].'</option>
                    <option value="'.$row["cqa7"].'">'.$row["cqa7"].'</option>
                    <option value="'.$row["cqa8"].'">'.$row["cqa8"].'</option>
                    <option value="'.$row["cqa9"].'">'.$row["cqa9"].'</option>
                    <option value="'.$row["cqa10"].'">'.$row["cqa10"].'</option>
                    <option value="'.$row["cqa11"].'">'.$row["cqa11"].'</option>
                    <option value="'.$row["cqa12"].'">'.$row["cqa12"].'</option>
                    <option value="'.$row["cqa13"].'">'.$row["cqa13"].'</option>
                    <option value="'.$row["cqa14"].'">'.$row["cqa14"].'</option>
                    <option value="'.$row["cqa15"].'">'.$row["cqa15"].'</option>
                    <option value="'.$row["cqa16"].'">'.$row["cqa16"].'</option>
                    <option value="'.$row["cqa17"].'">'.$row["cqa17"].'</option>
                    <option value="'.$row["cqa18"].'">'.$row["cqa18"].'</option>
                    <option value="'.$row["cqa19"].'">'.$row["cqa19"].'</option>
                    <option value="'.$row["cqa20"].'">'.$row["cqa20"].'</option>
                    <option value="'.$row["cqa21"].'">'.$row["cqa21"].'</option>

                   </select>';
       // $output .= '<tr><td colspan="10"><hr></td></tr>';
       $output .= '</div></div></div>';
       $quesno++;
  }  
  echo $output;
 }  
 ?>

Jquery ( Sending form fields value to php file) :-

$(document).ready(function() {

    // submit form
    $("#newleadform").unbind('submit').bind('submit', function() {

        $(".text-danger").remove();

        var form = $(this);

        // validation 
    var assettitle = $("#assettitle").val();
    var customquestion1 = $("#cq1").val();
    var customquestion2 = $("#cq2").val();
    var customquestion3 = $("#cq3").val();
    var customquestion4 = $("#cq4").val();
    var customquestion5 = $("#cq5").val();
    var customquestion6 = $("#cq6").val();
    var customquestion7 = $("#cq7").val();
    var customquestion8 = $("#cq8").val();
    var customquestion9 = $("#cq9").val();
    var customquestion10 = $("#cq10").val();
    var customquestion11 = $("#cq11").val();
    var customquestion12 = $("#cq12").val();
    var customquestion13 = $("#cq13").val();
    var customquestion14 = $("#cq14").val();
    var customquestion15 = $("#cq15").val();

        if(assettitle) {
            //submit the form to server
            $.ajax({
                url : '../pages/php_action/addnewlead/createlead.php',
                type : 'POST',
                data : form.serialize(),
                dataType : 'json',
                success:function(response) {

                    // remove the error 
                    $(".form-group").removeClass('has-error').removeClass('has-success');

                    if(response.success == true) {
                        $(".messages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
                          '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                          '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+
                        '</div>');

                        // reset the form
                        $("#newleadform")[0].reset();

                    } else {
                        $(".messages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
                          '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                          '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.messages+
                        '</div>');
                    }  // /else
                } // success  
            }); // ajax subit               
        } /// if


        return false;
    }); // /submit form for create member
}); // /add modal

createlead.php ( submit form fields value to database) :-

<?php 

require_once 'db_config.php';

//if form is submitted
if($_POST) {    
$validator = array('success' => false, 'messages' => array());

$assettitle= $_POST['assettitle'];
$customquestion1= $_POST['cq1'];
$customquestion2= $_POST['cq2'];
$customquestion3= $_POST['cq3'];
$customquestion4= $_POST['cq4'];
$customquestion5= $_POST['cq5'];
$customquestion6= $_POST['cq6'];
$customquestion7= $_POST['cq7'];
$customquestion8= $_POST['cq8'];
$customquestion9= $_POST['cq9'];
$customquestion10= $_POST['cq10'];
$customquestion11= $_POST['cq11'];
$customquestion12= $_POST['cq12'];
$customquestion13= $_POST['cq13'];
$customquestion14= $_POST['cq14'];
$customquestion15= $_POST['cq15'];





$sql = "INSERT INTO ltfs (assettitle, customquestion1, customquestion2, customquestion3, customquestion4, customquestion5, customquestion6, customquestion7, customquestion8, customquestion9, customquestion10, customquestion11, customquestion12, customquestion13, customquestion14, customquestion15) VALUES ('$assettitle', '$customquestion1', '$customquestion2', '$customquestion3', '$customquestion4', '$customquestion5', '$customquestion6', '$customquestion7', '$customquestion8', '$customquestion9', '$customquestion10', '$customquestion11', '$customquestion12', '$customquestion13', '$customquestion14', '$customquestion15')";


$query = $connect->query($sql);


if($query === TRUE) {
    $validator['success'] = true;
    $validator['messages'] = "Your lead successfully submited to quality team";     
} else {        
    $validator['success'] = false;
    $validator['messages'] = "Error while adding the lead information";
}

// close the database connection
$connect->close();

echo json_encode($validator);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think doing mistake here

if($query === TRUE)

You can do as

if($query){//you message}else{//error message}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...