如何使用PHP post将SPAN值从JSON存储到html表单

时间:2018-04-07 17:29:53

标签: javascript php jquery json ajax

我一直在处理一个表单,我正在使用JSON文件检索城市/州信息。当用户输入他们的PIN码时,我已经到达了可以看到相关城市/州的阶段。现在我坚持的一点是,在用户提交表单时将这些信息解析到数据库。

关于城市/州的信息在填写PIN码后立即填写在表格上的字段中,但我无法将此数据输入数据库,尽管尝试了多个选项(隐藏字段,尝试存储在变量中。)

到目前为止,表单很容易填写并且正在获取所有信息,除了在该字段中填充的详细信息。

我的表格如下;

    <?php
include("inc/dbconnect.php");

session_start();
$username=$_SESSION['username'];
if($username=="")
{
    header("location:index.php");
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add new lead</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="inc/jquery-ui.css">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<style>
  .ui-autocomplete-loading {
    background: white url("img/ui-anim_basic_16x16.gif") right center no-repeat;
  }
   .ui-autocomplete {
    max-height: 300px;
    overflow-y: auto;
    /* prevent horizontal scrollbar */
    overflow-x: hidden;
  }
  /* IE 6 doesn't support max-height
   * we use height instead, but this forces the menu to always be this tall
   */
  * html .ui-autocomplete {
    height: 100px;
  }
  </style>

<script src="inc/jquery.min.js"></script>
<script src="inc/jquery-ui.min.js"></script>

<script>
$(document).ready(
function () {
$( "#datepicker" ).datepicker({
changeMonth: true,//this option for allowing user to select month
changeYear: true //this option for allowing user to select from year range
});
}
);
</script>

<script>
$(document).ready(
function () {
$( "#datepicker2" ).datepicker({
changeMonth: true,//this option for allowing user to select month
changeYear: true //this option for allowing user to select from year range
});
}
);
</script>

<script>
$(document).ready(
function () {
$( "#datepicker3" ).datepicker({
changeMonth: true,//this option for allowing user to select month
changeYear: true //this option for allowing user to select from year range
});
}
);
</script>
</head>
<body>
<table width="900px" border="0" cellspacing="0" cellpadding="0">
  <tr>
      <td height="30"><?php include("inc/header2.php")?></td>
  </tr>
  <tr>
    <td style="padding:30px;"><form action="addlead.php" method="post" enctype="multipart/form-data" name="form" id="form">
      <table width="660" border="0" align="center" cellpadding="1" cellspacing="1" class="resultborder">
        <tr>
          <td width="684" colspan="2" align="center" valign="middle" class="resultheading2">Add  new lead</td>
        </tr>

        <tr>
          <td colspan="2" align="center" valign="middle" style="padding-top:5px;"><?php if(isset($msg)) {echo $msg;}?></td>
        </tr>
        <tr>
          <td colspan="2" align="center" valign="middle" style="padding-top:5px;"><table width="660" border="0" align="center" cellpadding="5" cellspacing="5">
            <tr>
              <td align="left" class="fieldhome">Lead source</td>
              <td align="left" valign="middle"><select name="source" id="source" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" required>
                  <option value="">Select lead source</option>
                    <option value="Sales ID">Sales ID</option>
                    <option value="Call back">Call Back</option>
                    <option value="Toll Free">Toll Free</option>
                    <option value="Walk-in">Walk-in</option>
                    <option value="Referral">Referral</option>
                    <option value="Exhibition">Exhibition Data</option>
                </select></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">CEP reference (if any)</td>
              <td align="left" valign="middle"><select name="cep_ref" id="cep_ref" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" >
                  <option value="-">Select CEP reference</option>
                  <option value="NA">Not Applicable</option>
                  <option value="Pallavi">Pallavi</option>
                  <option value="Pooja">Pooja</option>
                  <option value="Rashmi">Rashmi</option>
                  <option value="Savita">Savita</option>
                  <option value="Shaily">Shaily</option>
                </select>              </td>
            </tr>

            <tr>
              <td width="209" align="left" class="fieldhome">Lead date</td>
              <td width="416" align="left" valign="middle"><input id="datepicker" name="inq_date" required="required" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Call date</td>
              <td align="left" valign="middle"><input id="datepicker2" name="call_date" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Company Name</td>
              <td align="left" valign="middle"><input type="text" name="company" id="company" class="addformtextfield" /></td>
            </tr>

            <tr>
              <td align="left" class="fieldhome">Customers Name</td>
              <td align="left" valign="middle"><input type="text" name="name" id="name" required="required" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Mobile number</td>
              <td align="left" valign="middle"><input type="text" name="mobile" id="mobile" required="required" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Landline number</td>
              <td align="left" valign="middle"><input type="text" name="phone" id="phone" required="required" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Email</td>
              <td align="left" valign="middle"><input type="text" name="email" id="email" required="required" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Designation</td>
              <td align="left" valign="middle"><input type="text" name="designation" id="designation" class="addformtextfield" /></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Nature of Industry</td>
              <td align="left" valign="middle"><select name="nature_ind" id="nature_ind" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" >
                  <option value="-">Select Nature of Industry</option>
                  <option value="Trader">Trader</option>
                  <option value="Manufacturer">Manufacturer</option>
                  <option value="Individual">Individual</option>
              </select></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Address</td>
              <td align="left" valign="middle"><input type="text" name="address" id="address" class="addformtextfield" /></td>
            </tr>

            <tr>
              <td align="left" class="fieldhome">Pin Code</td>
              <td align="left" valign="middle">
              <div>
              <input type="text" id="pin" name="pin" placeholder="Enter pincode" width="40%"><span style="color:red; font-size:9px;"> Enter at least 3 digit to show auto-complete.</span></div>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
  $(function() {
   $( "#pin" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "pincodes.php",
          dataType: "json",
          data: {
            q: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
      minLength: 3,  // Set minum input length
      select: function( event, ui ) {
            //do something on select event
            var vl = ui.item.id;      
            var data = vl.split("-");
            console.log(data);
            $("#city").html(data[2]);
            $("#taluka").html(data[3]);
            $("#state").html(data[4]);
        //console.log(ui.item); // ui.item is  responded json from server
      },
      open: function() {
                 // D0 something on open event.
      },
      close: function() {
               // Do omething on close event
      }
    });
  });
  </script>
</td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Taluka:</td>
              <td align="left" valign="middle"><span id="taluka"></span> <?php $taluka='<span id="taluka"></span>'?></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">City:</td>
              <td align="left" valign="middle"><span id="city"></span>    <input type="hidden" name="city" id="city" value="" />
</td>
            </tr>

            <tr>
              <td align="left" class="fieldhome">State:</td>
              <td align="left" valign="middle"><span id="state" name="state"></span></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Potential</td>
              <td align="left" valign="middle"><select name="potential" id="potential" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" >
                  <option value="-">Select lead potential</option>
                  <option value="High">High</option>
                  <option value="Medium">Medium</option>
                  <option value="Low">Low</option>
              </select></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Purchases what all?</td>
              <td align="left" valign="middle"><textarea rows="2" cols="32" name="what_purchases" style="padding:2px; font-family:Arial; font-size:18px;"></textarea></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Other important contacts</td>
              <td align="left" valign="middle"><textarea rows="2" cols="32" name="other_contacts" style="padding:2px; font-family:Arial; font-size:18px;"></textarea></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Turn over</td>
              <td align="left" valign="middle"><select name="turnover" id="turnover" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" >
                  <option value="-">Select Customer's Turnover</option>
                  <option value="Less than 50L">Less than 50L</option>
                  <option value="1Cr to 2Cr">1Cr - 2Cr</option>
                  <option value="2Cr to 5Cr">2Cr-5Cr</option>
                  <option value="5Cr to 10Cr">5Cr-10Cr</option>
                  <option value="10Cr plus">10Cr+</option>
              </select></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Total Employees</td>
              <td align="left" valign="middle"><select name="employees" id="employees" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" >
                  <option value="-">Total number of employees</option>
                  <option value="1">1</option>
                  <option value="2 to 5">2-5</option>
                  <option value="5 to 10">5-10</option>
                  <option value="10 to 100">10-100</option>
                  <option value="100 to 500">100-500</option>
                  <option value="500 plus">500+</option>
              </select></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">New customer?</td>
              <td align="left" valign="middle"><select name="new_cust" id="new_cust" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" required>
                  <option value="">Please choose if a new lead?</option>
                  <option value="Yes">Yes</option>
                  <option value="Existing-lat">Existing-LAT</option>
                  <option value="Existing-other">Existing-Other</option>
              </select></td>
            </tr>
            <tr>
              <td align="left" class="fieldhome">Follow up date</td>
              <td align="left" valign="middle"><input id="datepicker3" name="follow_up" class="addformtextfield" /></td>
            </tr>

            <tr>
              <td align="left" class="fieldhome">Lead Status</td>
              <td align="left" valign="middle"><select name="lead_status" id="lead_status" style="width:308px; height:30px; padding:2px; font-family:Arial; font-size:14px;" >
                  <option value="-">Select lead status</option>
                  <option value="Won-online">Won-online</option>
                  <option value="Won-offline">Won-offline</option>
                  <option value="Lost">Lost</option>
                  <option value="Regret">Regret</option>
                  <option value="Disqualified">Disqualified</option>
                  <option value="Not reachable">Not reachable</option>
                  <option value="Hold">Hold</option>
              </select></td>
            </tr>

            <tr>
              <td align="left" class="fieldhome">Remarks</td>
              <td align="left" valign="middle"><textarea rows="8" cols="32" name="remarks" style="padding:2px; font-family:Arial; font-size:18px;"></textarea></td>
            </tr>

            <tr>
              <td align="center" class="fieldhome">&nbsp;</td>
              <td align="left" valign="middle"><input type="submit" name="submit" value="Submit" style="width:80px; font-size:18px; padding:8px; font-weight:bold; font-family:Arial; color:#333333;" /></td>
            </tr>
          </table></td>
        </tr>
      </table>
    </form></td>
  </tr>
  <tr>
    <td><?php include "inc/footer.php"?></td>
  </tr>
</table>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

问题可能出在&#34; multipart / form-data&#34;。尝试使用&#34; urlencoded&#34;而不是&#34; multipart / form-data&#34; (从表单中删除enctype="multipart/form-data")。

答案 1 :(得分:0)

不确定我是否正确,但我认为当你这样做时

$("#city").html(data[2]);
   $("#taluka").html(data[3]);
   $("#state").html(data[4]);

您实际上是使用data[]更改HTML本身,而不是

的值
<input type="hidden" name="city" id="city" value="" />