javascript调用php数组变量问题
我在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='=$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û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éponses sont incomplètes (une puce par question doit-être cochée)"); } }); });// script pr limiter les caractÃ?¨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 à 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中
把 换成 =$str?> 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?
不过错误信息的内容与你的描述不一致,不好说什么了
既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能
把 换成 =$str?> 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?
不过错误信息的内容与你的描述不一致,不好说什么了
既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能
确实是菜鸟,而且程序不是我写的, 只是发现了一个bug要修改, 还在学习中
求大神指教要怎么做传进去呢
把 换成 =$str?> 出现错误
这表示你的 php 版本低于 5.4 且没有打开短标记功能
你使用了 xdebug 扩展,不会连 xdebug 的报告格式也不知道吧?
不过错误信息的内容与你的描述不一致,不好说什么了
既然你使用了 JQuery,那么就会要使用他的无刷新功能
但是你把 php 动态生成的数据直接嵌入在 js 代码中,这在流程上是不对的
也不能发挥出 JQuery 的性能
确实是菜鸟,而且程序不是我写的, 只是发现了一个bug要修改, 还在学习中
求大神指教要怎么做传进去呢
直接在js 的前面调用
在
$(document).ready(function(){ 这个之前定义一个 var str =
在JS里面直接调用。
把 换成 =$str?> 出现错误
这表示你的 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

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Alipay PHP ...

JWT는 주로 신분증 인증 및 정보 교환을 위해 당사자간에 정보를 안전하게 전송하는 데 사용되는 JSON을 기반으로 한 개방형 표준입니다. 1. JWT는 헤더, 페이로드 및 서명의 세 부분으로 구성됩니다. 2. JWT의 작업 원칙에는 세 가지 단계가 포함됩니다. JWT 생성, JWT 확인 및 Parsing Payload. 3. PHP에서 인증에 JWT를 사용하면 JWT를 생성하고 확인할 수 있으며 사용자 역할 및 권한 정보가 고급 사용에 포함될 수 있습니다. 4. 일반적인 오류에는 서명 검증 실패, 토큰 만료 및 대형 페이로드가 포함됩니다. 디버깅 기술에는 디버깅 도구 및 로깅 사용이 포함됩니다. 5. 성능 최적화 및 모범 사례에는 적절한 시그니처 알고리즘 사용, 타당성 기간 설정 합리적,

PHP 개발에서 견고한 원칙의 적용에는 다음이 포함됩니다. 1. 단일 책임 원칙 (SRP) : 각 클래스는 하나의 기능 만 담당합니다. 2. Open and Close Principle (OCP) : 변경은 수정보다는 확장을 통해 달성됩니다. 3. Lisch의 대체 원칙 (LSP) : 서브 클래스는 프로그램 정확도에 영향을 미치지 않고 기본 클래스를 대체 할 수 있습니다. 4. 인터페이스 격리 원리 (ISP) : 의존성 및 사용되지 않은 방법을 피하기 위해 세밀한 인터페이스를 사용하십시오. 5. 의존성 반전 원리 (DIP) : 높고 낮은 수준의 모듈은 추상화에 의존하며 종속성 주입을 통해 구현됩니다.

시스템이 다시 시작된 후 UnixSocket의 권한을 자동으로 설정하는 방법. 시스템이 다시 시작될 때마다 UnixSocket의 권한을 수정하려면 다음 명령을 실행해야합니다.

기사는 PHP 5.3에 도입 된 PHP의 LSB (Late STATIC BING)에 대해 논의하여 정적 방법의 런타임 해상도가보다 유연한 상속을 요구할 수있게한다. LSB의 실제 응용 프로그램 및 잠재적 성능

PHP 개발에서 PHP의 CURL 라이브러리를 사용하여 JSON 데이터를 보내면 종종 외부 API와 상호 작용해야합니다. 일반적인 방법 중 하나는 컬 라이브러리를 사용하여 게시물을 보내는 것입니다 ...

기사는 입력 유효성 검사, 인증 및 정기 업데이트를 포함한 취약점을 방지하기 위해 프레임 워크의 필수 보안 기능을 논의합니다.

phpstorm에서 CLI 모드를 디버그하는 방법은 무엇입니까? PHPStorm으로 개발할 때 때때로 CLI (Command Line Interface) 모드에서 PHP를 디버그해야합니다 ...
