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

Javascript jQuery.ajax未将值传递给php变量,javascript,Javascript,这是我的代码,用于将值从表单输入到javascript变量,并希望将其传输到php变量。但它不输出任何内容 <script type="text/javascript"> function swapImage(id, primary, secondary,ide) { src = document.getElementById(id).src; var allNone = true; var arr = new Array();

这是我的代码,用于将值从表单输入到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';
    }

函数交换页(id、主、辅、ide){
src=document.getElementById(id).src;
var-allNone=true;
var arr=新数组();
arr=document.getElementById(ide);
if(src.match(主)){
对于(var i=arr.length-1;i>=0;i-=1){
如果(arr.value!=“无”){
jQuery(函数(){
jQuery.ajax({
url:'blog.php',
键入:“POST”,
数据:{
jM:$(“#跳转菜单选项:选定”).val(),
jM2:$(“#jumpMenu2选项:选定”).val(),
jM3:$(“#jumpMenu3选项:选定”).val(),
jM4:$(“#jumpMenu4选项:选定”).val(),
jM5:$(“#jumpMenu5选项:选定”).val(),
jM6:$(“#jumpMenu6选项:选定”).val(),
jM7:$(“#jumpMenu7选项:选定”).val(),
jM8:$(“#jumpMenu8选项:选定”).val(),
jM9:$('#jumpMenu9选项:选中').val(),
},
成功:功能(结果){
警报(结果);
},
错误:函数(结果){
console.log('Error:');
控制台日志(结果);
}
});
});  
document.getElementById(id).src=secondary;
allNone=false;
打破
} 
}
如果(全部无)
{
警报(“您必须指定项目的大小!”);
}
}否则{
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>

价格:
尺寸:
输入项目大小
小的
中等
大的
特大型
XX大
价格:
尺寸:
输入项目大小
小的
中等
大的
特大型
XX大

我认为您的问题可能出在选择器上(我还没有测试它)。您需要的是select元素的值,而不是选项的值。所以我不介意

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


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

首先,在提交数据后,您想对数据做什么?我怀疑是否只是提醒结果(); 第二,不管答案是什么,都应该在AJAX的成功回调函数中为其编写代码


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

你听说过阵列吗?当你有10个变量要以完全相同的方式处理时,它们真的很漂亮。听说过但不知道如何在这里应用它看起来你可以使用
.serialize()
jQuery方法,只需将整个表单传递给服务器。抱歉,老实说,我只是一个初学者,试图将我从网络和书籍中获得的代码结合起来。请你解释一下。serialize是如何以一种更容易理解的方式工作的。我不能完全理解你给出的链接上的描述。它有相同的输出。但是当我被引导到“blog.php”页面,好吧,你明白了。我从stackoverflow最近的问题中得到了这些代码,老实说,我不知道如何使用ajax发布这些变量,以便blog.php可以访问这些javascript变量。好吧,那么,请在单击“提交”后确切地告诉我你想要做什么,如果您单击“提交”,它将被一个新的提交图像按钮替换:这是代码文档。getElementById(id).src=secondary;。。在替换过程中,来自标记的值将存储在这些变量中,下面是代码:jM:$(“#jumpMenu option:selected”).val(),我有9/下拉菜单。因此,如果单击“新建提交图像”按钮,存储在这些变量上的值将传递到php变量,然后是blog.p
$('#jumpMenu6').val()