2017-08-13 113 views
0
  1. 第一种形式就是客户可以得到所有产品的品牌型号和价格为单选按钮与有ID的值在数据库如何处理多个表单?,提交时停止重新加载页面?

  2. 客户端点击提交。

  3. PHP要求提交按钮

  4. 如果isset它回声第二种形式,其中客户端可以为产品,他或她选择添加规格isset名。形式是输入型文本和文本区域的

  5. 那么PHP请求第二种形式

提交按钮isset名字,但问题是,当客户按提交第二种形式

如果第一种形式是未设置那么代码的其余部分应该检查,第二种形式不存在,因为这两种形式的代码大部分退出工作,我可以得到,如果我没有发现停止页面重新加载提交

我尝试使用AJAX但MySQL查询会抛出一个输入为空的错误。

JavaScript代码:

<script> 
$(document).ready(function() { 
    $('.link').click(function(){ 
    $.post('ajax1.php?strana='+$(this).attr('strana'), function(odgovor) { 
     $('#odgovor').html(odgovor); 
    }); 
    }); 
}); 
</script> 

PHP代码:

echo "<div class='container' id='cars'> 
    <div class='row'> 
     <div class='col-md-4 col-md-offset-4 text-center'> 
     <form action='' method='post' class='loginForm' id='cmsC' name='cmsC' enctype='multipart/form-data'> 
      <div class='input-group'>"; 
      $sql="select * from produkt "; 
      $rez=mysqli_query($db,$sql); 
      while($red = mysqli_fetch_object($rez)) 
      { 
      echo "<input type='radio' name='proizvod' value='$red->id'> ". $red->marka." ". $red->naziv." ". $red->cena."<br>"; 
      } 
      echo "<input type='submit' id='btnsubmit1' name='btnsubmit1' class='form-control' value='Chouse Product' > 
     </div> 
     </form> 
    </div> 
    </div> 
</div>"; 

if (isset($_POST['btnsubmit1'])) { 
    $id = $_POST['proizvod']; 

    echo "<div class='container'> 
    <div class='row'> 
    <div class='col-md-4 col-md-offset-4 text-center'> 
     <form action='' method='post' class='loginForm' id='cms' name='cms' enctype='multipart/form-data'> 
     <div class='input-group clear'> 
      <input type='text' id='dotm' name='dotm' class='form-control' 
      placeholder='Description Of The Motherboard (ASUS H81M-R/C/SI)'> 

      <input type='text' id='top' name='top' class='form-control' 
      placeholder='Type Of Processor (Intel® Core™ i3 Processor)'>"; // there is bunch more of this type for input type 

      if (isset($_POST['submit'])) { 
        // Variable with data from form 
        $description_of_the_motherboard = $_POST['dotm']; 
        $type_of_processor = $_POST['top']; 
        $processor_description = $_POST['pd']; 
        $type_of_graphics_card = $_POST['togc']; 

        if(strlen($description_of_the_motherboard)!="") { 
        if(!preg_match("/[^\w\s,.\\\'\"\-\/]/", $description_of_the_motherboard) { 
         $sql = "insert query"; 
         mysqli_query($db, $sql); 

        } else echo "<script> 

        alert('Your input can not have special characters '); 

        </script>"; 
      }    
      } 
} 

回答

1

只需添加一个标志,以避免重复请求。

实施例:

$(document).ready(function() { 
    var iAmProcessing = false; 
    $('.link').click(function(){ 
    if (iAmProcessing === true) { 
     return; 
    } 
    iAmProcessing = true; 
    $.post('ajax1.php?strana='+$(this).attr('strana'), function(odgovor) { 
     iAmProcessing = false; 
     $('#odgovor').html(odgovor); 
    }); 
    }); 
}); 
0

数据最后是到第二个参数阵列。

<script> 
    $(document).ready(function() { 
     $('.link').click(function(){ 
      $.post('ajax1.php',{strana:$(this).attr('strana'),function(odgovor){ 
       $('#odgovor').html(odgovor); 
      }); 
     }); 
    }); 
</script>