jQuery.ajax未将值传递给php变量

jQuery.ajax not passing value to php variable

本文关键字:php 变量 值传 ajax jQuery      更新时间:2024-05-16

这是我的代码,用于将值从表单输入到javascript变量,并希望将其传输到php变量。。但是它什么也不输出。

<script type="text/javascript">

function swapImage(id, primary, secondary,ide) {        

    src = document.getElementById(id).src;
    var allNone = true;
    var arr = new Array();
    arr = document.getElementById(ide);   
    if (src.match(primary)) {
            for(var i = arr.length - 1; i >= 0; i-=1) {                 
            if (arr.value != "none"){                                                                                                            
                        jQuery(function(){                                
                               jQuery.ajax({
                                    url: 'blog.php',
                                    type: 'POST',
                                    data: {
                                        jM:  $('#jumpMenu  option:selected').val(),
                                        jM2: $('#jumpMenu2 option:selected').val(),
                                        jM3: $('#jumpMenu3 option:selected').val(),
                                        jM4: $('#jumpMenu4 option:selected').val(),
                                        jM5: $('#jumpMenu5 option:selected').val(),
                                        jM6: $('#jumpMenu6 option:selected').val(),
                                        jM7: $('#jumpMenu7 option:selected').val(),
                                        jM8: $('#jumpMenu8 option:selected').val(),
                                        jM9: $('#jumpMenu9 option:selected').val(),
                                    },
                                    success: function(result){
                                        alert(result);
                                    },
                                    error: function(results) {
                                        console.log('Error:');
                                        console.log(results);
                                    }
                                });
                        });  
                        document.getElementById(id).src=secondary;
                        allNone = false;
                        break;                                                         
                    } 
            }
              if (allNone)
                {
                    alert("You must specify the item's size!");
                }
    } else {                                     
        location.href = 'blog.php';
    }

}

这是另一个网页的php代码

Did you type "<?php 
   $jM1 = isset($_POST['jM'])? $_POST['jM'] : '' ; 
    $jM2 = isset($_POST['jM2'])? $_POST['jM2'] : '';
    $jM3 = isset($_POST['jM3'])? $_POST['jM3'] : '';
    $jM4 = isset($_POST['jM4'])? $_POST['jM4'] : '';
    $jM5 = isset($_POST['jM5'])? $_POST['jM5'] : '';
    $jM6 = isset($_POST['jM6'])? $_POST['jM6'] : '';
    $jM7 = isset($_POST['jM7'])? $_POST['jM7'] : '';
    $jM8 = isset($_POST['jM8'])? $_POST['jM8'] : '';
    $jM9 = isset($_POST['jM9'])? $_POST['jM9'] : '';
    echo $jM1."<br />".$jM2."<br />".$jM3; 
    ?>" for?

输出如下:"你键入了"吗?"。这是更新的。。我可以从警报消息框中获得结果,但我无法从blog.php 中输出

这是html代码:

 <th height="464" scope="col"><img src="images/three.jpg" width="304" height="363" class = "tilt pic" />
        <table width="200" border="0" align="center">
          <tr>
            <th width="63">Price:</th>
            <th width="127"><?php echo $final_array[0][1]; ?></th>
          </tr>
          <tr>         
            <th>Size:</th>
            <form method="post" name="form" id="form">
            <th><select name="jumpMenu" id="jumpMenu" class="jM">
                <option value ="none">Enter Item Size</option>
                <option value ="6">Small</option>
                <option value ="8">Medium</option>
                <option value ="10">Large</option>
                <option value ="12">Extra Large</option>
                <option value ="14">XX Large</option>
              </select>
            </th>               
                <th><img                      
                    src="images/submit.gif" name="tadi"
                    id="tadi"
                    title="View Order"
                    onclick="swapImage(
                      'tadi',
                      'images/submit.gif',
                      'images/so.gif',
                      'jumpMenu'
                    )"
                  />           
          </th>
            </form>
          </tr>
      </table>

      </th>
      <th scope="col"><img src="images/two.jpg" width="304" height="363" class = "tilt pic" />
         <table width="200" border="0" align="center">
          <tr>
            <th width="63">Price:</th>
            <th width="127"> <?php echo $final_array[0][1]; ?></th>
          </tr>
          <tr>
            <th>Size:</th>
            <form method="post" name="form" id="form" action="blog.php">
            <th>  <select name="jumpMenu" id="jumpMenu2" class="jM2">
                <option value ="none">Enter Item Size</option>
                <option value ="6">Small</option>
                <option value ="8">Medium</option>
                <option value ="10">Large</option>
                <option value ="12">Extra Large</option>
                <option value ="14">XX Large</option>
              </select>
            </th>
            <th><img
                    id="change"
                    onclick="swapImage(
                      'change',
                      'images/submit.gif',
                      'images/so.gif',
                      'jumpMenu2'
                    )"                      
                    src="images/submit.gif"
                    title="View Order"
                  />  
            </th>
            </form>
          </tr>
      </table>
           </th>

我认为问题可能出在选择器上(我还没有测试过)。您需要的是选择元素的值,而不是选项的值。也是如此

$('#jumpMenu6 option:selected').val()

尝试:

$('#jumpMenu6').val()

看起来您正在尝试将同步行为与异步行为相结合。

首先,在提交数据后,你想对数据做什么?我怀疑这只是警告结果();第二,不管答案是什么——你应该在AJAX的成功回调函数中为它编写代码。

此外,我可能错了,但从你的问题中,我知道你将访问blog.php并期望看到输出变量?!这种情况永远不会发生,因为你不会仅仅通过访问浏览器中的blog.php url来传递$_POST['jm*']变量——这就是为什么你使用AJAX来POST这些变量。