简体   繁体   English

PHP,AJAX和服务器-客户端的挫败感

[英]PHP, AJAX, and server-client frustrations

I have a table that's generated from php. 我有一个从php生成的表。 The way I've done this is probably not the most efficient way to do it since I wrote it all myself and I'm not an expert. 因为我是自己写的,而且我不是专家,所以我这样做的方法可能不是最有效的方法。

Everything starts when the user pastes part numbers into a search box on a previous page, which is then sent here to return.php under the variable lines . 当用户将零件号粘贴到上一页的搜索框中时,一切就开始了,然后将其发送到此处的变量lines下的return.php中。

return.php return.php

$c = $_POST['c'];
if (!$_SESSION['lines']) {
  $_SESSION['lines'] = $_POST['lines'];
}
$partNumber = array(); //define $partNumber as array

$x = -1;
$supplierQuery      = "SELECT distinct supplier, quotePartNumber FROM allparts WHERE quotePartNumber = '$q'" ;
$supplierResult     = mysqli_query($con, $supplierQuery);

foreach ($_SESSION['lines'] as $q) {
 $x = $x + 1; // each time we loop through this, x++

   while ($row = mysqli_fetch_array($supplierResult)) {
     $partNumber[]   = $row['quotePartNumber'];
     $customerQuery  = "SELECT DISTINCT quoteCustomer FROM $supplier where quotePartNumber = '$q'";

    if (!$c) { // $c becomes set once a user types in an end customer - without that, we want ALL generic info to be returned.
        $costQuery      = "SELECT * FROM $supplier where quotePartNumber = '$partNumber[$x]' ORDER BY quoteCost ASC LIMIT 1" ;
      } else {            
        $costQuery      = "SELECT * FROM $supplier where quotePartNumber = '$partNumber[$x]' and quoteCustomer = '$c' ORDER BY quoteCost ASC LIMIT 1" ;
       }
       $getCustomer      = mysqli_query($con, $customerQuery);
    }

later on in my table, I have this: 后来在我的桌子上,我有这个:

<td><?= $partNumber[$x] ?></td>
<td><?= $cost ?></td>
<td>
  <select class="btn btn-danger" onChange="selectCustomerCMR(this.value)">
  <option value="" disabled selected><?php if($c) { print $c; } else { print "Select Purchasing Customer";} ?></option>                  

  <?php                              
  while ($row = mysqli_fetch_array($getCustomer)) {                                 

  $customerName = $row['quoteCustomer'];          
  ?>
    <option><?= $customerName ?></option>                        

  <?php
  }
  ?>          
  </select>    
</td>

Any change to the dropdown will launch this script: 下拉菜单的任何更改都将启动以下脚本:

<script>
function selectCustomerCMR(str) {
var id = str;

$.ajax({
      type: 'POST',
      url: 'return.php',
      data: {'c':id,'lines':lines},        
        success:function(data){
        $("#info").html(data);
        }
  });        
}
</script>

What I'm trying to do 我想做什么

Let's say my generated table has 3 rows, with part numbers 假设我生成的表格有3行,其中包含部件号 在此处输入图片说明

There is a drop-down to allow the user to select a specific customer. 有一个下拉列表允许用户选择特定的客户。 When the user clicks on this, the script takes that value and uses AJAX to send it back to the same page (return.php), which then grabs it using the $c = $_POST['c']; 当用户单击它时,脚本将使用该值,并使用AJAX将其发送回同一页面(return.php),然后使用$c = $_POST['c'];捕获$c = $_POST['c']; code. 码。

My Issue 我的问题

When return.php loads a "second time" with a value for $c , I don't know how to make it so that the line that the user selected gets changed . 当return.php使用$c的值加载“第二次”时,我不知道如何制作它,以便用户选择的行被更改 Right now, anytime I select a customer from a line's drop-down, return.php reloads, and it assigns that customer to the FIRST row, ignoring all the other rows. 现在,无论何时我从行的下拉列表中选择一个客户,return.php都会重新加载,它将该客户分配给FIRST行,而忽略所有其他行。

I specifically created $partNumber as an array and used $x so I could increase the value of x each time the foreach loop iterated. 我专门创建了$partNumber作为数组,并使用$x所以每次foreach循环迭代时,我都可以增加x的值。 This worked, so of the three lines in the above table, the first one is $partNumber[0] and the second one is $partNumber[1] , etc... But I don't know how to get that information into the javascript function and send it back to the page when it reloads, so that I can then change my SQL query to ONLY action when the condition is right for that line... 这样$partNumber[0] ,因此在上表的三行中,第一行是$partNumber[0] ,第二行是$partNumber[1]$partNumber[1] ... 但是我不知道如何将这些信息放入javascript函数,并在重新加载时将其发送回页面,以便我可以在条件适合该行的情况下将SQL查询更改为“仅行动” ...

Thanks for reading, and thanks for any help! 感谢您的阅读,也感谢您的任何帮助!

Consider changing your <select> code to this: 考虑将您的<select>代码更改为此:

<select class="btn btn-danger" data-x="<?= $x ?>" onChange="selectCustomerCMR(this)">

Then, your Ajax code can be changed to this: 然后,您的Ajax代码可以更改为此:

function selectCustomerCMR(select) {
    var id = select.value, x = select.getAttribute("data-x");

    $.ajax({
        type: 'POST',
        url: 'return.php',
        data: { c: id, lines: lines, x: x },        
        success: function(data){
            // Update!
        }
    });        
}

That way, your PHP can get both c and x . 这样,您的PHP可以同时获取cx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM