将变量Ajax传递到PHP同一页面 Ajax测试 每页项目数

将变量Ajax传递到PHP同一页面 Ajax测试 每页项目数,php,jquery,ajax,Php,Jquery,Ajax,你的代码正在工作!但是,您必须将PHP放在HTML之前并停止脚本(或者在另一个脚本中-请参阅下面的注释) 发布表单时,PHP脚本将回显$\u POST['num-items'],并停止执行以返回Javascript 提交表单后,请查看控制台,您将只获得您提交的值 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Ajax Testi

你的代码正在工作!但是,您必须将PHP放在HTML之前并停止脚本(或者在另一个脚本中-请参阅下面的注释)

发布表单时,PHP脚本将回显
$\u POST['num-items']
,并停止执行以返回Javascript

提交表单后,请查看控制台,您将只获得您提交的值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax Testing</title>
    <!-- Jquery Library -->
    <script `enter code here`src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

    <form action="" method="post" id="itemsPag">
        <div class="form-group">
           <label for="items-per-page">Items Per Page</label>
            <select name="num-items" id="items-per-page" class="form-control">
                <?php for($i = 5 ; $i < 55 ; $i+=5): ?>
                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                <?php endfor; ?>
            </select>
            <input type="submit" name="submit">
         </div> 
    </form>     

<script>        

        $( document ).ready( function() {           
            $('#itemsPag').on('submit',function (e) {

            $.ajax({
                    type: 'post',
                    url: '',
                    data: $(this).serialize(),
                    success: function (data) {                      
                        console.log(data);
                    }
                });
            e.preventDefault();     
            });         
});

</script>
    <?php echo (isset($_POST['num-items'])) ? $_POST['num-items'] : "NO WORKING"; ?>
</body>
</html>

Ajax测试
每页项目数

我认为最好使用不同的脚本来处理AJAX请求。试着把这些东西混合在一起可能会很快导致非常混乱的代码。同意@Mike。。。我将ajax php部分堆在页面顶部的唯一一次,是在我很忙的时候,做一个基本的后端“工具”旋转。但是前端和生产设备应该分开,以便清晰和易于维护。但是,这是一个简单的工作示例。非常感谢您的帮助和回答:)。我真的很感激我还有一个问题。设置exit的问题是,这个脚本下面的其他php脚本没有执行。我真正想做的是抓住它的价值并在下面使用它。我想知道设置另一个php页面并将值发送到该页面,然后返回到当前页面是否可以解决问题?在这种情况下,如何将该值从php进程页面返回到当前页面?我不知道你是否理解我。谢谢:)
<?php 
   // Here the code is before the HTML, but your can put it in 
   // another script (and be sure that the url parameter of 
   // your jQuery.ajax() point on it).
   if (isset($_POST['num-items'])) {
      echo $_POST['num-items'] ;
      exit; // stop the script to avoid to return the HTML to 
      // your Javascript.
   }
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax Testing</title>
    <!-- Jquery Library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

    <form action="" method="post" id="itemsPag">
        <div class="form-group">
           <label for="items-per-page">Items Per Page</label>
            <select name="num-items" id="items-per-page" class="form-control">
                <?php for($i = 5 ; $i < 55 ; $i+=5): ?>
                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                <?php endfor; ?>
            </select>
            <input type="submit" name="submit">
         </div> 
    </form>     

    <script type="text/javascript">        

        $( document ).ready( function() {           
            $('#itemsPag').on('submit',function (e) {

            $.ajax({
                    type: 'post',
                    url: '',
                    data: $(this).serialize(),
                    success: function (data) {                      
                        console.log(data); // Now, here you will have only the PHP block.
                    }
                });
            e.preventDefault();     
            });         
       });

    </script>
</body>
</html>