Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

html - How can I get form values by loops in PHP

How can I get only the test# inputs without the email and password by

<form method="get">
    <input type="email" placeholder="email" name="email">
    <input type="password" placeholder="passsword" name="passsword">
    <input type="text" placeholder="test 1" name="test#1">
    <input type="text" placeholder="test 2" name="test#2">
    <input type="text" placeholder="test 3" name="test#3">
    <input type="text" placeholder="test 4" name="test#4">
    <input type="text" placeholder="test 5" name="test#5">
    <input type="text" placeholder="test 6" name="test#6">
    <input type="text" placeholder="test 7" name="test#7">
    <input type="text" placeholder="test 8" name="test#8">
    <input type="text" placeholder="test 9" name="test#9">
    <input type="text" placeholder="test 10" name="test#10">
    <button name="submit" type="submit">submit</button>
</form>

I tried foreach but it gets all values


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could filter by array key:

$testFields = array_filter($_POST, function($key){
    return strpos($key, 'test#') === 0;
}, ARRAY_FILTER_USE_KEY);

But I would instead suggest (as written in the comments) that you name your fields "test[]" and iterate over those:

foreach($_POST['test'] as $val){

}

PHP automatically gives you an array of fields with "[]" at the end of their name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...