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

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条回答 默认 最新

  • from.. 2019-11-06 23:51
    关注

    Hard to guess since we're missing part of the code but I'd start by checking in your browser's network tab that ref1 was actually posted with the right value. The casting it to int() in php should work. I'd guess that since you get the value 0 the variable contains a letter or another character.

    Also, you should really look into using prepared statements, otherwise your code is vulnerable to sql injections.

    Side note, to simplify your validation you could use empty() which will do isset + compare with "" all in one.

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败