【问题标题】:Passing Jquery Variable to PHP for database submission将 Jquery 变量传递给 PHP 以进行数据库提交
【发布时间】:2017-08-20 11:02:14
【问题描述】:

我正在尝试将 Jquery 变量传递给 php,以便将变量值提交到我的数据库。我的 php 代码、html 代码和我的 jquery 代码都在同一个名为 entrysurvey.php 的代码文件中。我有一个单选按钮,当它被单击时,它是一个图像,该属性用于定义一个 jquery 变量。这很成功,代码的警报部分显示了与图像对应的正确变量号。但是,我还没有成功地利用 AJAX 将其传递给 php。我尝试了许多不同的 onSubmit 函数,但到目前为止没有任何效果。任何帮助将不胜感激。

我的 Jquery 代码:

<script type="text/javascript" src="javascript/jquery-3.2.1.min.js"> </script>

	<script type="text/javascript">

	$(document).ready(function(){

			var value
			$('img').click(function(){
		    $('.selected').removeClass('selected');
		    $(this).addClass('selected');
		   // var value = $(this).val();
		   // alert(value);
			if($(this).attr("alt") == "first image"){

				value = "1";
				alert(value);




				
			} else if($(this).attr("alt") == "second image"){

				value = "2";
				alert(value);

			}else if($(this).attr("alt") == "third image"){

				value="3"
				alert(value);
			}else if($(this).attr("alt")== "fourth image"){

				value="4"
				alert(value);
			}else if($(this).attr("alt")=="fifth image"){

				value="5";
				alert(value);
   				}else if($(this).attr("alt")=="sixth image"){

				value="6";
				alert(value);
			}else if($(this).attr("alt")=="seventh image"){

				value="7";
				alert(value);
			}



					$('.submitButton').click(function() { 
if (value != null) { //value is a variable so you can write it like this
    $.post('entrysurvey.php', {val: value}, function(response) { 
        alert(response); 
    }); 
} 
});





	

			});








			

			

	});






	</script>

HTML 代码:

     <form action="entrysurvey.php" method="POST" id="target">
     
    Select the image that you feel resembles your relationship with your pair the most: <br> <br>

      <table  cellpadding="20">
    			<tbody>
    					<tr>
    							<td><input type="radio"       name="oneness" value = "1" class="oneness"  > <br> <br> <br> <img src="images/1.png" height="200px" width="200px" class="selectedImage" alt="first image" > <br> <br></td>
    								<td> <input type="radio" name="oneness" value = "2"  class="oneness" > <br> <br> <img src="images/2.png" height = "200px" width="200px" class="selectedImage" alt="second image" > <br> <br></td>
    								<td><input type="radio" name="oneness" value = "3"  class="oneness"  > <br> <br> <img src="images/3.png" height = "200px" width="200px" class="selectedImage" alt="third image" > <br> <br></td>
    								<td>&nbsp;</td>
    							</tr>
    						</tbody>
    					</table>

    					<table cellpadding="20">
    						<tbody>
    							<tr>
    								<td><input type="radio" name="oneness" value = "4"  class="oneness"  > <br> <br> <img src="images/4.png" height = "200px" width="200px" class="selectedImage" alt="fourth image" > <br> <br></td>
    								<td><input type="radio" name="oneness" value = "5"  class="oneness"  > <br> <br> <img src="images/5.png" height = "200px" width="200px" class="selectedImage" alt="fifth image" > <br> <br></td>
    								<td><input type="radio" name="oneness" value = "6"  class="oneness"  > <br> <br> <img src="images/6.png" height = "200px" width="200px" class="selectedImage" alt="sixth image"> <br> <br></td>
    								<td><input type="radio" name="oneness" value = "7"  class="oneness" > <br> <br> <img src="images/7.png" height = "200px" width="200px" class="selectedImage" alt="seventh image"> <br> <br></td>
    							</tr>
    						</tbody>
    					</table>
              
    <input type="submit" name="submit" value="Submit" class="submitButton">  
    			 </form>

最后,这是我用来尝试将其传递给 php 的函数。

$('.submitButton').click(function(){ if($(value) != null){ $.post('entrysurvey.php', 'val=' + $(value).val(), function(response){ alert(response); }); } }); 

这是我的 PHP 代码:

!-- begin sn-p: js hide: false console: true babel: false -->

<!-- language: lang-php-->

不幸的是,应该取 val 值的 ios 仍然没有任何反应,这是我得到的错误/响应:

enter image description here

【问题讨论】:

  • 你的ajax函数在哪里?
  • $('.submitButton').click(function(){ if($(value) != null){ $.post('entrysurvey.php', 'val=' + $( value).val(), function(response){ alert(response); }); } });就是这个!
  • 请使用 ajax 函数更新您的问题,以便其他人可以正常阅读。
  • 感谢我编辑它的建议:)

标签: php jquery mysqli


【解决方案1】:

您必须发布您的 ajax 代码!我猜,问题在于你的 value 变量是在 if 语句中声明的。这意味着您的变量的范围仅在您的 if 语句中。试着在任何声明之外声明它,让我说

$(document).ready(function(){
   var value;
   //the other part of code down here

您必须知道您将值变量发布为字符串,而不是整数,以防万一这可能服务

【讨论】:

  • $('.submitButton').click(function(){ if($(value) != null){ $.post('entrysurvey.php', 'val=' + $( value).val(), function(response){ alert(response); }); } });这是我将其传递给 php 的函数,我还编辑了代码以确保变量不在 if 语句中,仍然无法正常工作!
【解决方案2】:

我编辑了你的 ajax 代码,因为有一些错误!

$('.submitButton').click(function() { 
    if (value != null) { //value is a variable so you can write it like this
        $.post('entrysurvey.php', {val: value}, function(response) { 
            alert(response); 
        }); 
    } 
});

现在将变量发送到服务器的正确语法是以 json 格式发送,例如 {val: value}!所以你将变量 val 发送到服务器,这个变量的值是 value 变量的值。因此,由于您使用的是 post 方法,因此您可以在 entrysurvey.php 文件中使用类似的内容; $variable_name = $_POST['val']; 这样php文件就会得到你要发送的值!!!

【讨论】:

  • 我编辑了上面的代码以匹配您的代码,但不幸的是 val 的值仍然为空
  • 好改变输入类型提交;让它只是一个按钮。当它被点击时,这就是 jquey ajax 部分进来的地方
  • 可以去掉form标签,使用ajax时,不强制使用form标签向服务器传输数据
猜你喜欢
  • 2014-03-20
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多