目錄
回复讨论(解决方案)
Q1 
Q3?
首頁 後端開發 php教程 javascript调用php数组变量问题

javascript调用php数组变量问题

Jun 23, 2016 pm 01:34 PM

我在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="/static/imghw/default1.png"  data-src="images/logo_bytel.png"  class="lazy"   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 id="Q-nbsp">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 id="Q">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 id="Q">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
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1677
14
CakePHP 教程
1431
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP和Python:深入了解他們的歷史 PHP和Python:深入了解他們的歷史 Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHP和框架:現代化語言 PHP和框架:現代化語言 Apr 18, 2025 am 12:14 AM

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

為什麼要使用PHP?解釋的優點和好處 為什麼要使用PHP?解釋的優點和好處 Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles