weixin_33747129
2019-11-06 23:36
采纳率: 0%
浏览 20

AJAX帖子编号作为字符串

I am new learner and need your support.

i have Echo $ref; ($ref = number) and passing value through AJAX to another page for some operations but this value is always passing as string and if i am using intval($_post(['$ref1']) function it is giving zero value.

php code user will fill the form and click on create new belwo code..

<?php
 session_start();

  require "includes/config.php";
  include('header.php');

?>



<div class='container'>

<div style='margin-top:10px'>
  <a href='welcome.php' class='btn btn-primary'>User Profile</a>
  <a href='invsummarymaindd.php' class='btn btn-primary'>Back Invoice Summary</a>
  <a href='logout.php' class='btn btn-danger'>Sign Out of Your Account</a>
 </div>

 <div class='row col-md-12' style='margin-top:15px'>

 <div class='col-md-3'>
   <input type='text' class='form-control' name='vendor' id='vendor' placeholder='Supplier Name'>
  </div>

  <div class='col-md-3'>
   <input type='text' class='form-control' name='vendor_email' id='vendor_email' placeholder='email'>
   </div>

   <div class='col-md-3'>
   <input type='text' class='form-control' name='vendor_person' id='vendor_person' placeholder='Name Of Person'>
   </div>

   <div class='col-md-3'>
   <input type='text' class='form-control' name='vendor_phone' id='vendor_phone' placeholder='Phone Number'>
   </div>
</div>

<div class='col-md-3'>
 <button name='create_vendor' class='btn btn-primary' id='create_vendor'> Create Invoice </button>
 <input type='hidden' name='login_user_id' id='login_user_id'>
</div>

</div>
<div id="maindata"></div>

<br/>  <br/>

<br><br>
  <script type="text/javascript" src="js/operation.js"></script>

ajax code full code - ajax to pass data from one page to another

$('#create_vendor').on('click',function() {

  var vendor  = $('#vendor').val();
  var vendor_email  = $('#vendor_email').val();
  var vendor_person  = $('#vendor_person').val();
  var vendor_phone  = $('#vendor_phone').val();

        $.ajax({
            type: 'POST',
            url: 'vendor_info.php',
            data: 'vendor='+vendor+'&vendor_email='+vendor_email+'&vendor_person='+vendor_person+'&vendor_phone='+vendor_phone,
            success: function(ref2){
               show_details(ref2);
            }
        });
        return false;
 });

 function show_details(ref2){
   $.ajax({

       type: 'POST',
       url: 'vendor_info.php',
       data: 'ref1='+ref2,
       success: function(data){
         console.log(ref2);
         console.log(typeof(ref2));
         $('#maindata').html(data);
       }
    });
    console.log(typeof(ref2));
  }

php code to for the inserting data to mysql

<?php

if(!empty($_POST['vendor']) && !empty($_POST['vendor_email']) && !empty($_POST['vendor_person']) && !empty($_POST['vendor_phone'])){
  $max_sql = "SELECT MAX(ID) as max_number FROM supplier";
  $max_result = $mysqli->query($max_sql);

   $max_number = 1;
   while($res=$max_result->fetch_assoc()) {
     $max_number =  $res['max_number'];
     $ref2  = $max_number;
   }

$sql_add = "INSERT INTO  supplier set
        SupplierName = '".$_POST['vendor']."',
        Email = '".$_POST['vendor_email']."',
        PersonName = '".$_POST['vendor_person']."',
        CC = '".$ref2."',
        phone = '".$_POST['vendor_phone']."'";

$result = $mysqli->query($sql_add);
echo $ref2;

}
  ?>

after data is inserted data will be shown in the page by passing $ref2 value through ajax, which is number but it is becomming string and when i am using in sql as per below no data is appearing but i use hard number to test it is working

<?php
 session_start();

  require "includes/config.php";
  include('header.php');

?>


<?php
if( isset($_POST['ref1']) && ($_POST['ref1'] != "") ){

$q = $_POST['ref1'];

//to check status of $q
echo $q;
echo gettype($q);

if (is_numeric($q)) { echo "Yes"; } else { echo "No"; }
if (is_string($q)) { echo "Yes"; } else { echo "No"; }

        $mquery = "SELECT * FROM  supplier WHERE ID='".$q."'";
        $result = $mysqli->query($mquery);


      echo "<table class='table'>
        <tr>
        <th>ID</th>
        <th>SupplierName</th>
        <th>Email</th>
        <th>PersonName</th>
        <th>phone</th>
        </tr>";

      while ($row = $result->fetch_assoc()){
        echo "<tr>";
        echo "<td>".$row['ID']."</td>";
        echo "<td>".$row['SupplierName']."</td>";
        echo "<td>".$row['Email']."</td>";
        echo "<td>".$row['PersonName']."</td>";
        echo "<td>".$row['phone']."</td>";
        echo "</tr>";

     }

      echo "</table>";


looking for the support and code is full

1条回答 默认 最新

相关推荐 更多相似问题