search
HomeBackend DevelopmentPHP Tutorial想在php中输出在js中的数组,但是php中输不出来,求解答

<script></script>
<script> <br />$function () <br />{ <br /> $('#btn').click(function (){ <br /> <br /> var checkArray = document.getElementsByName("tj[]"); <br /> var n=0; <br /> var arr=new Array(); <br /> //alert(checkArray); <br /> for (var i =0; i < checkArray.length; i++) <br /> { <br /> if(checkArray[i].checked) <br /> { <br /> //选中的操作 <br /> arr[n]=i; <br /> n++; <br /> <br /> } <br /> <br /> } <br /> var obj = { <br /> <br /> data : JSON.stringify(arr) <br /> <br /> } <br /> $.post('./xueshengxuanke.php',obj).then(function (data){ <br /> console.log(data); <br /> }) <br /> }) <br /> }) <br /></script>
这是js生成数组并且传递

       $data=json_decode($_post['data']);
      echo $data[0];
      echo '
';
      echo $data[1];

     

      
   ?>
这是php接收


回复讨论(解决方案)

$_post['data']  你把这个东西var_dump 打出来 看看是什么样子的 就知道怎么弄了啊 

$_POST 不是 $_post

我试试

$_post['data']  你把这个东西var_dump 打出来 看看是什么样子的 就知道怎么弄了啊 

$_post['data']  你把这个东西var_dump 打出来 看看是什么样子的 就知道怎么弄了啊 



打印出来之后是NULL

$_POST 不是 $_post



改成大写打印出来也是NULL

print_r($_POST);
看看是什么

print_r($_POST);
看看是什么


还是一个空的ARRAY()

你 ajax 没有传过来,有怨不得 php 了

你 ajax 没有传过来,有怨不得 php 了



那是要怎么改呢,JS也是初步接触不太会

你先看看 obj 中是否有值

对了,你不会是在 PhpStorm 中调试吧?

你先看看 obj 中是否有值

对了,你不会是在 PhpStorm 中调试吧?



我不知道是不是data : JSON.stringify(arr); 这句话的问题 这个函数貌似没有被认可
是在sublime中调试
输出obj也没有值

你本来就可以这样用的
$.post('./xueshengxuanke.php', arr).then(function (data){

php 中 print_r($_POST);

你本来就可以这样用的
$.post('./xueshengxuanke.php', arr).then(function (data){

php 中 print_r($_POST);




我这么做了
得到的数组是arr[0]->on 我要的不是on
而是arr[]对应的值

你本来就可以这样用的
$.post('./xueshengxuanke.php', arr).then(function (data){

php 中 print_r($_POST);




Array ( [tj] => Array ( [0] => on [1] => on ) )
这个就是输出的结果 但是我的目的是输出被选复选框所在的行数,而这个行数是存在arr[]这个数组里面的,我要的是arr[]=值而不是on 不知道我描述的清楚吗

            for (var i =0; i             { 
                if(checkArray[i].checked)
                {
                    //选中的操作    
                    arr[n]=i;
                    n++;       
                }
            }
alert(arr) //看到什么

            for (var i =0; i             { 
                if(checkArray[i].checked)
                {
                    //选中的操作    
                    arr[n]=i;
                    n++;       
                }
            }
alert(arr) //看到什么



这样是看不到数组内容的,之前我是用循环让他变成字符串可以输出看到。


不要开玩笑


不要开玩笑



真的没看玩笑 弹不出来窗口
var str = "";

           for(var m = 0; m             {
               
             /str += " " + arr[m];
            

            }
          alert("checked index:"+str);
这样我才能弹出框输出来

Array ( [ tj] => Array ( [0] => on [1] => on ) )
请你如实的反映情况,就你贴出的代码,绝不可能出现 tj 项

Array ( [ tj] => Array ( [0] => on [1] => on ) )
请你如实的反映情况,就你贴出的代码,绝不可能出现 tj 项



<script> <br />//var arr=new array(); <br /> <br />function check() <br />{ <br /> <br /> //console.log(document.getElementsByName("tj[]")); <br /> <br /> var checkArray = document.getElementsByName("tj[]"); <br /> //alert(checkArray); <br /> var arr=[]; <br /> var n=0; <br /> for (var i =0; i < checkArray.length; i++) <br /> { <br /> if(checkArray[i].checked == true) <br /> { <br /> //选中的操作 <br /> <br /> //alert(i); <br /> // document.write(i); <br /> arr[n]=i; <br /> n++; <br /> //console.log("arr[n]"); <br /> <br /> <br /> } <br /> } <br /> <br /> $.post('./xueshengxuanke.php',arr).then(function (data){ <br /> alert(data)}); <br /> <br />} <br /> <br /></script>


   print_r($_POST);
   ?>



这是之后修改过的代码 
第一张图是不选择光点提交
第二张图是点了第一个第二个所显示的数组

没有骗你截图为证啊

的的 js 中有 arr[n]=i;
我要看到你在提交之前 alert(arr) 的截图!
明白吗?

的的 js 中有 arr[n]=i;
我要看到你在提交之前 alert(arr) 的截图!
明白吗?






不好意思啊之前没充分理解你的意思

既然 arr 只有一个元素(5)
那么 php怎么可能输出 Array ( [tj] => Array ( [0] => on [1] => on ) ) 呢?

既然 arr 只有一个元素(5)
那么 php怎么可能输出 Array ( [tj] => Array ( [0] => on [1] => on ) ) 呢?




可能我之前表达不清楚
 如果不点提交按钮 arr数组是不会有数据的

这次我是点了一个复选框 所以输出一个值
如果我点的是两个 输出就是两个值
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)