javascript调用php数组变量问题

WBOY
發布: 2016-06-23 13:34:17
原創
1268 人瀏覽過

我在php里面定义的了一个数组, 想在内部的javascript函数中调用, 可是怎么都报错, 求各位大神帮忙看看!感激不尽!
php里面:

<?php$qst=array("R2","R3","R7");$index_qst=3;-----------------------------------$qst[$index_qst]="R1";		$index_qst++; // 动态的统计数组-----------------------------------$str = json_encode($qst); ?>
登入後複製


Js:
<script type="text/javascript">	$(document).ready(function(){		$("#send").click(function(){                               			var empty =0;			var nbr_qst = <?php echo "$str";?>; //这一行一直报错		for (var i in nbr_qst) {									if (!$("input[name='i']").is(':checked')){					empty=1;				}
登入後複製


打开网页原代码报错的部分显示如下
$(document).ready(function(){		$("#send").click(function(){                    			var test =0;                  			var empty =0;			var str = <br />
登入後複製


快要崩溃了!!!! 求救!!


回复讨论(解决方案)

我将js里面改用了var str='';
var nbr_qst = JSON.parse(str); 突然发现网页代码中间有一堆不知道css... 求指教

	$(document).ready(function(){		$("#send").click(function(){                    			var test =0;                  			var empty =0;			var str='<br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: str in C:\Users\btang\xampp\htdocs\QuestionaireV5\QuestionnaireV5\QuestionnaireV.php on line <i>11</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0020</td><td bgcolor='#eeeeec' align='right'>160720</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\Users\btang\xampp\htdocs\QuestionaireV5\QuestionnaireV5\QuestionnaireV.php' bgcolor='#eeeeec'>..\QuestionnaireV.php<b>:</b>0</td></tr></table></font>';			var nbr_qst = JSON.parse(str);		for (var i in nbr_qst) {									if (!$("input[name='i']").is(':checked')){					empty=1;				}			}
登入後複製

很明显那只是PHP的错误信息:
Notice: Undefined variable: str in C:\Users\btang\xampp\htdocs\QuestionaireV5\QuestionnaireV5\QuestionnaireV.php on line 11

建议你贴出完整的代码。

很明显那只是PHP的错误信息:
Notice: Undefined variable: str in C:\Users\btang\xampp\htdocs\QuestionaireV5\QuestionnaireV5\QuestionnaireV.php on line 11

建议你贴出完整的代码。



    <?php  include('header.php'); ?><script type="text/javascript">	$(document).ready(function(){		$("#send").click(function(){                    			var test =0;                  			var empty =0;			var nbr_qst = eval(<?=$json_qst;?>);		for (var i in nbr_qst) {									if (!$("input[name='i']").is(':checked')){					empty=1;				}			}			$("#radio").find("input[type=radio]").each(function(i)                 {                                                 if ($(this).is(':checked') && (!$.trim($("#textare").val()))){						var value = $(this).val();						if (value.indexOf("Non")==0 || value.indexOf("Pas")==0) {							test = 1;						}					}								}			);			if (empty ==0) {				if (test== 0) {					$("#radio").submit();				}				else{         $( "#dialog-confirm" ).dialog({      resizable: false,      height:200,      width:400,      modal: true,      buttons: {        "Oui": function() {       		$( this ).dialog( "close" );        },        Non: function() {            $("#radio").submit();        }      }    });				/*var r = confirm("Etes-vous s&Atilde;&raquo;r de vouloir valider votre questionnaire sans commentaires ?");					if (r == true) {						$("#radio").submit();					} */				}			}			else{                                   $( "#dialog-confirm1" ).dialog({      resizable: false,      height:200,      width:400,      modal: true,      buttons: {        "Fermer": function() {      $(this).dialog("close");        }             }    });				//alert("Une ou plusieurs r&Atilde;&copy;ponses sont incompl&Atilde;&uml;tes (une puce par question doit-&Atilde;&ordf;tre coch&Atilde;&copy;e)");			}			                   		});	});// script pr limiter les caract&Atilde;?&Acirc;&uml;res ds le champ commentairevar ns6=document.getElementById&&!document.allfunction restrictinput(e, theform, maxlength) {  if(theform.value.length>=maxlength) {    var pressedkey = /[a-zA-Z0-9\.\,\/]/;    if (pressedkey.test(String.fromCharCode(e.keyCode))) {      if(e.preventDefault) { e.preventDefault(); }      e.returnValue = false;      return false;    }  }} function countlimit(e, theform, maxlength, text, text2) {    var lengthleft = maxlength - theform.value.length;    if (lengthleft <= 1) {    caractere = text;    }   else {        caractere = text2;    }  document.getElementById("limite").innerHTML = '<strong>'+ lengthleft +'</strong> '+ caractere;} function displaylimit(theid, thelimit, text, text2) {    var theform = document.getElementById(theid);    var limit_text = '<span id=limite><strong>'+thelimit+'</strong> '+ text2 +'</span>';  		document.write(limit_text);  if (theform.addEventListener) {        theform.addEventListener('keypress', function(e) {      var that = this;      restrictinput(e, that, thelimit);    }, true);        theform.addEventListener('keyup', function(e) {      var that = this;      countlimit(e, that, thelimit, text, text2);    }, true);    } else {    theform.onkeypress = function(e) {      e = e || window.event;      var that = this;      return restrictinput(e, that, thelimit);    };    theform.onkeyup = function(e) {      e = e || window.event;      var that = this;      countlimit(e, that, thelimit, text, text2);    };  }}function displaylimit_rem(theid, thelimit, text, text2) {    var theform = document.getElementById(theid);    var limit_text = '<span id=limite_rem style="font-size:10px"><strong>'+thelimit+'</strong> '+ text2 +'</span>';  		document.write(limit_text);  if (theform.addEventListener) {        theform.addEventListener('keypress', function(e) {      var that = this;      restrictinput(e, that, thelimit);    }, true);        theform.addEventListener('keyup', function(e) {      var that = this;      countlimit_rem(e, that, thelimit, text, text2);    }, true);    } else {    theform.onkeypress = function(e) {      e = e || window.event;      var that = this;      return restrictinput(e, that, thelimit);    };    theform.onkeyup = function(e) {      e = e || window.event;      var that = this;      countlimit_rem(e, that, thelimit, text, text2);    };  }}function countlimit_rem(e, theform, maxlength, text, text2) {    var lengthleft = maxlength - theform.value.length;    if (lengthleft <= 1) {    caractere = text;    }   else {        caractere = text2;    }  document.getElementById("limite_rem").innerHTML = '<strong>'+ lengthleft +'</strong> '+ caractere;}          </script><!--    <div id="pre_header">            <div id="header">                <img  src="images/logo_bytel.png" border="0"    style="max-width:90%"  style="max-width:90%" alt="javascript调用php数组变量问题" >                <p>ENQUETE DE SATISFACTION</p>            </div>                    </div>--><?phpinclude ('connexion.php');include ('question.class.php');global $connection;$qst=array("R2","R3","R7");$index_qst=3;echo '<div class="site clearfix body">';echo '<form action="enregistrer.php" method=POST id=radio name=form1  class="clearfix" >';$Fixe = filter_input ( INPUT_POST, "idFixe" );$idMobile = filter_input ( INPUT_POST, "idMobile" );$opInstall = filter_input ( INPUT_POST, "opInstall" );$vga = filter_input ( INPUT_POST, "vga" );// echo $vga;// echo $opInstall;$q = new question ();if (isset ( $idMobile )) {	echo '<input type=hidden name=ref_clt value=' . $idMobile . '>';	echo '<input type=hidden name=ref_type value="Mobile">';} else if (isset ( $Fixe )) {	if (isset ( $opInstall )) {		if ($opInstall == 'OUI' || $opInstall == 'oui') {			echo '<input type=hidden name=ref_clt value=' . $Fixe . '>';			echo '<input type=hidden name=ref_type value="BboxAvecInstall">';		} else if ($opInstall == 'non' || $opInstall == 'NON') {			echo '<input type=HIDDEN name=ref_clt value=' . $Fixe . '>';			echo '<input type=hidden name=ref_type value="BboxSansInstall">';		}	} else if (isset ( $vga )) {		if ($vga == 'Crea') {			echo '<input type=hidden name=ref_clt value=' . $Fixe . '>';			echo '<input type=hidden name=ref_type value="VgaCrea">';		} else if ($vga == 'Repr') {			echo '<input type=hidden name=ref_clt value=' . $Fixe . '>';			echo '<input type=hidden name=ref_type value="VgaRepr">';		}	} else {		echo '<input type="hidden" name="ref_clt" value=' . $Fixe . '>';		echo '<input type="hidden" name="ref_type" value="Fixe">';	}}// echo '<fieldset class=field>';if (isset ( $Fixe ) || isset ( $idMobile )) {	if (! isset ( $opInstall )) {		echo '<h4 class="qst">Q1 </h4>';		echo '<p class="input_p"><span class="wrapInput">                                                   <input id="Q1_1" name="R1" value="Oui, tout a fait" type="radio" aria-labelledby="radio_1-ariaLabel"/>		<label for="Q1_1" id="radio_1-ariaLabel">Oui, tout &Atilde;  fait</label>							 						</span>';		echo '<span class="wrapInput">		<input id="Q1_2" name="R1" value="Non, pas assez" type="radio" aria-labelledby="radio_2-ariaLabel" />		<label for="Q1_2" id="radio_2-ariaLabel">Non, pas assez</label>					</span></p>';		echo '</br></br></br>';		echo "<hr></hr>";		$qst[$index_qst]="R1";		$index_qst++;		}}// ///////Question2////////echo '<h4 class="qst">Q2</h4>';echo '<p class="input_p" ><span class="wrapInput">                                                   <input id="Q1_1" name="R2" value="Oui" type="radio"  aria-labelledby="radio_1-ariaLabel" /><label for="Q1_1" id="radio_1-ariaLabel">Oui</label>					 						</span>';echo '<span class="wrapInput">          <input id="Q1_2" name="R2" value="Non" type="radio"  aria-labelledby="radio_2-ariaLabel" /><label for="Q1_2" id="radio_2-ariaLabel">Non</label>					</span></p>';echo '</br></br></br>';// ///////Question3////////echo '<h4 class="qst">Q3?</h4>';echo '<p class="input_p"><span class="wrapInput">                                                   <input id="Q1_1" name="R3" value="Oui" type="radio"  aria-labelledby="radio_1-ariaLabel" />            		<label for="Q1_1" id="radio_1-ariaLabel">Oui</label>							 						</span>';echo '<span class="wrapInput">          <input id="Q1_2" name="R3" value="Non" type="radio"  aria-labelledby="radio_2-ariaLabel" />			<label for="Q1_2" id="radio_2-ariaLabel">Non</label>					</span></p>';echo '</br></br></br>';echo "<hr></hr>";$json_qst = json_encode($qst); </body></html>
登入後複製

代码太长了 贴不完整 重要的部分都在这里了
整个页面的完整代码如上,根据url的参数用if判断那几个问题要echo,所以用数组获取当前显示的问题名传入到js中

换成 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?

不过错误信息的内容与你的描述不一致,不好说什么了

既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能

换成 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?

不过错误信息的内容与你的描述不一致,不好说什么了

既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能



确实是菜鸟,而且程序不是我写的, 只是发现了一个bug要修改, 还在学习中
求大神指教要怎么做传进去呢


换成 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?

不过错误信息的内容与你的描述不一致,不好说什么了

既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能



确实是菜鸟,而且程序不是我写的, 只是发现了一个bug要修改, 还在学习中
求大神指教要怎么做传进去呢



直接在js 的前面调用


$(document).ready(function(){ 这个之前定义一个 var str =

在JS里面直接调用。



换成 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?

不过错误信息的内容与你的描述不一致,不好说什么了

既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能



确实是菜鸟,而且程序不是我写的, 只是发现了一个bug要修改, 还在学习中
求大神指教要怎么做传进去呢



直接在js 的前面调用


$(document).ready(function(){ 这个之前定义一个 var str =

在JS里面直接调用。



这样还是会报那一句的错

是不是JS哪块需要加收一个标签。就好像smarty的literal 标签一样。才能解析。
不行的话 你这样把那个数据放到一个 这样。然后在JS中通过DOM元素来获取。

是不是JS哪块需要加收一个标签。就好像smarty的literal 标签一样。才能解析。
不行的话 你这样把那个数据放到一个 这样。然后在JS中通过DOM元素来获取。



$json_qst = json_encode($qst);echo '<input type="hidden" name ="json_qst" value="<?php echo $json_qst;?>">';
登入後複製
登入後複製

var nbr_qst = document.getElementByname("json_qst");
登入後複製
登入後複製

js这里还是报错 是我写的不对吗


是不是JS哪块需要加收一个标签。就好像smarty的literal 标签一样。才能解析。
不行的话 你这样把那个数据放到一个 这样。然后在JS中通过DOM元素来获取。



$json_qst = json_encode($qst);echo '<input type="hidden" name ="json_qst" value="<?php echo $json_qst;?>">';
登入後複製
登入後複製

var nbr_qst = document.getElementByname("json_qst");
登入後複製
登入後複製

js这里还是报错 是我写的不对吗



报什么错,不行加QQ群说吧:231566327
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!