Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 如何批量输入数据,并执行?

如何批量输入数据,并执行?

Jun 23, 2016 pm 01:03 PM

各位大神,是这样的,我要把若干数据逐组传递给adc.php执行;我知道一组一组的传可以用以下代码方式。但是如果我想传递很多组,不需要手工一组一组的输入,怎么办?

<script> <br />$(document).ready(function(){ <br />$("#submit").click(function(){ <br />var name=$("#name").val(); <br />var scores=$("#scores").val(); <br /> location.href=”adc.php?name=”+name+”&scores=”+scores; <br />} <br /></script>




姓名: 

得分: 




例如,以下5组数据,如果分5次分别输入,上述程序可以,但是,有无可能我一次性将下表贴入文本框,然后程序自动识别,并逐组执行?谢谢大神。
小王 100
小张 121
小刘 541
张三 555
李四 410


回复讨论(解决方案)

思路应该是 数组 json传递 接收解析 foreach循环

可以啊  你把名字全部输出一个框中用个统一的分隔符分隔号  直接传递给php后explode一下就行 或者你在前端用string.splite()也行

比如你输入了: 小王 100 小张 121 小刘 541 张三 555 李四 410 都用空额分隔好
前端处理的话

var str = ' 小王 100 小张 121 小刘 541 张三 555 李四 410';var data = str.splite(" ");var urlparam = "?a=1";//多传一个没用的参数保持数据格式 方便下面循环for(var i = 0 ; i<data.length;1++){if(i%2 == 0){     urlparam += "&name[]="+data[i];}else{     urlparam += "&scores[]="+data[i]}} location.href="abc.php"+urlparam;
Copy after login

后台获得的是一个name数组和scores数组
或者你直接传递给后台
?data=小王 100 小张 121 小刘 541 张三 555 李四 410
后台explode()一下 根据上面类似的方法循环也可以

可以使用textarea保存,一行一个用户
然后,PHP根据换行来解释。

例如:
html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head>  <meta http-equiv="content-type" content="text/html;charset=utf-8">  <title> 提交 </title> </head> <body>  <form name="form1" id="myform" method="post" action="server.php">    <p>数据:<textarea name="data"></textarea></p>    <p><input type="submit" name="b1" onclick="fsubmit()" value="提交"></p>  </form> </body></html>
Copy after login


server.php
<?php$data = $_POST['data'];$arr = explode("\r\n", $data);$result = array();for($i=0,$len=count($arr); $i<$len; $i++){    if($arr[$i]!=''){        list($name, $score) = explode(',', $arr[$i]);        $result[] = array(            'name' => $name,            'score' => $score        );    }}print_r($result);?>
Copy after login


在textarea中输入
小王,100
小张,121
小刘,541
张三,555
李四,410

提交后,PHP获取到
Array(    [0] => Array        (            [name] => 小王            [score] => 100        )    [1] => Array        (            [name] => 小张            [score] => 121        )    [2] => Array        (            [name] => 小刘            [score] => 541        )    [3] => Array        (            [name] => 张三            [score] => 555        )    [4] => Array        (            [name] => 李四            [score] => 410        ))
Copy after login

谢谢各位大神提供的思路和方法,我没有全部学会,但我大概学会了2楼的。谢谢大家。

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles