php how to loop through existing values and put them together?
I have the following code:
<form action="" method="post">
<input type="text" name="text[0]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
The #AddMore adds input elements (with jQuery) where the number iterates. So when you click it once you get:
<form action="" method="post">
<input type="text" name="text[0]">
<input type="text" name="text[1]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
When it is submitted, I want to grab the values of the existing inputs and add it to my $message string. I thought of it something like this:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" // . $extraInput; ???
for ($x = 1; $x < 20; $x++) { // 20 = max number of inputs
if(isset($_POST['text[$x]']) && $_POST['text[$x]'] != '')
$extraInput-$x .= "Text number: " . $x + 1 " contains: " . $_POST['text[$x]'] . "rn"; // how to add this to $message?
else
$extraInput-$x .= "";
I know my php is not correct, but I put it here anyway so you would understand my intentions better. The right way to use it is what I like to know/learn.
For example, when I click 'Add more fields' twice. I want php to put the following string into $message:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" . "Text number: 2 contains: " . $_POST['text[1]'] . "rn" . "Text number: 3 contains: " . $_POST['text[2]'] . "rn";
That's what I finally want to mail using:
mail($to,$subject,$message);
php
add a comment |
I have the following code:
<form action="" method="post">
<input type="text" name="text[0]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
The #AddMore adds input elements (with jQuery) where the number iterates. So when you click it once you get:
<form action="" method="post">
<input type="text" name="text[0]">
<input type="text" name="text[1]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
When it is submitted, I want to grab the values of the existing inputs and add it to my $message string. I thought of it something like this:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" // . $extraInput; ???
for ($x = 1; $x < 20; $x++) { // 20 = max number of inputs
if(isset($_POST['text[$x]']) && $_POST['text[$x]'] != '')
$extraInput-$x .= "Text number: " . $x + 1 " contains: " . $_POST['text[$x]'] . "rn"; // how to add this to $message?
else
$extraInput-$x .= "";
I know my php is not correct, but I put it here anyway so you would understand my intentions better. The right way to use it is what I like to know/learn.
For example, when I click 'Add more fields' twice. I want php to put the following string into $message:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" . "Text number: 2 contains: " . $_POST['text[1]'] . "rn" . "Text number: 3 contains: " . $_POST['text[2]'] . "rn";
That's what I finally want to mail using:
mail($to,$subject,$message);
php
1
Doesnt PHP return an array for$_POST['text']
?
– DanielM
Feb 19 '16 at 13:21
add a comment |
I have the following code:
<form action="" method="post">
<input type="text" name="text[0]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
The #AddMore adds input elements (with jQuery) where the number iterates. So when you click it once you get:
<form action="" method="post">
<input type="text" name="text[0]">
<input type="text" name="text[1]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
When it is submitted, I want to grab the values of the existing inputs and add it to my $message string. I thought of it something like this:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" // . $extraInput; ???
for ($x = 1; $x < 20; $x++) { // 20 = max number of inputs
if(isset($_POST['text[$x]']) && $_POST['text[$x]'] != '')
$extraInput-$x .= "Text number: " . $x + 1 " contains: " . $_POST['text[$x]'] . "rn"; // how to add this to $message?
else
$extraInput-$x .= "";
I know my php is not correct, but I put it here anyway so you would understand my intentions better. The right way to use it is what I like to know/learn.
For example, when I click 'Add more fields' twice. I want php to put the following string into $message:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" . "Text number: 2 contains: " . $_POST['text[1]'] . "rn" . "Text number: 3 contains: " . $_POST['text[2]'] . "rn";
That's what I finally want to mail using:
mail($to,$subject,$message);
php
I have the following code:
<form action="" method="post">
<input type="text" name="text[0]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
The #AddMore adds input elements (with jQuery) where the number iterates. So when you click it once you get:
<form action="" method="post">
<input type="text" name="text[0]">
<input type="text" name="text[1]">
<a id="AddMore">Add more fields</a>
<input type="submit" value="submit">
</form>
When it is submitted, I want to grab the values of the existing inputs and add it to my $message string. I thought of it something like this:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" // . $extraInput; ???
for ($x = 1; $x < 20; $x++) { // 20 = max number of inputs
if(isset($_POST['text[$x]']) && $_POST['text[$x]'] != '')
$extraInput-$x .= "Text number: " . $x + 1 " contains: " . $_POST['text[$x]'] . "rn"; // how to add this to $message?
else
$extraInput-$x .= "";
I know my php is not correct, but I put it here anyway so you would understand my intentions better. The right way to use it is what I like to know/learn.
For example, when I click 'Add more fields' twice. I want php to put the following string into $message:
$message = "Text number: 1 contains: " . $_POST['text[0]'] . "rn" . "Text number: 2 contains: " . $_POST['text[1]'] . "rn" . "Text number: 3 contains: " . $_POST['text[2]'] . "rn";
That's what I finally want to mail using:
mail($to,$subject,$message);
php
php
edited Nov 12 '18 at 4:43
Cœur
17.5k9103145
17.5k9103145
asked Feb 19 '16 at 13:16
remkovdmremkovdm
558
558
1
Doesnt PHP return an array for$_POST['text']
?
– DanielM
Feb 19 '16 at 13:21
add a comment |
1
Doesnt PHP return an array for$_POST['text']
?
– DanielM
Feb 19 '16 at 13:21
1
1
Doesnt PHP return an array for
$_POST['text']
?– DanielM
Feb 19 '16 at 13:21
Doesnt PHP return an array for
$_POST['text']
?– DanielM
Feb 19 '16 at 13:21
add a comment |
2 Answers
2
active
oldest
votes
If your <input type="text">
have the attribute name
as text[0]
, text[1]
, etc, in your php code they values are not in $_POST[text[n]]
, but in $_POST[text][n]
.
So, you can obtain desired output in this way:
$message = '';
foreach( $_POST['text'] as $key => $val )
$message .= "Text number: ".($key+1)." contains: $valrn";
Or — if you want skip empty values — in this way:
$message = '';
$index = 1;
foreach( $_POST['text'] as $val )
if( $val )
$message .= "Text number: ".($index++)." contains: $valrn";
Edit: please also note that there is no need to check is $_POST[text][n]
if set, because all elements of an array are set, either if they are empty.
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
add a comment |
Here is a way to do it :
if(0 < sizeof($_POST['text']))
$message = '<ol>';
foreach($_POST['text'] as $text)
if(false === empty($text))
$message .= "<li>".$text."</li>";
$message = '</ol>';
else
$message = "void";
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35506253%2fphp-how-to-loop-through-existing-values-and-put-them-together%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your <input type="text">
have the attribute name
as text[0]
, text[1]
, etc, in your php code they values are not in $_POST[text[n]]
, but in $_POST[text][n]
.
So, you can obtain desired output in this way:
$message = '';
foreach( $_POST['text'] as $key => $val )
$message .= "Text number: ".($key+1)." contains: $valrn";
Or — if you want skip empty values — in this way:
$message = '';
$index = 1;
foreach( $_POST['text'] as $val )
if( $val )
$message .= "Text number: ".($index++)." contains: $valrn";
Edit: please also note that there is no need to check is $_POST[text][n]
if set, because all elements of an array are set, either if they are empty.
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
add a comment |
If your <input type="text">
have the attribute name
as text[0]
, text[1]
, etc, in your php code they values are not in $_POST[text[n]]
, but in $_POST[text][n]
.
So, you can obtain desired output in this way:
$message = '';
foreach( $_POST['text'] as $key => $val )
$message .= "Text number: ".($key+1)." contains: $valrn";
Or — if you want skip empty values — in this way:
$message = '';
$index = 1;
foreach( $_POST['text'] as $val )
if( $val )
$message .= "Text number: ".($index++)." contains: $valrn";
Edit: please also note that there is no need to check is $_POST[text][n]
if set, because all elements of an array are set, either if they are empty.
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
add a comment |
If your <input type="text">
have the attribute name
as text[0]
, text[1]
, etc, in your php code they values are not in $_POST[text[n]]
, but in $_POST[text][n]
.
So, you can obtain desired output in this way:
$message = '';
foreach( $_POST['text'] as $key => $val )
$message .= "Text number: ".($key+1)." contains: $valrn";
Or — if you want skip empty values — in this way:
$message = '';
$index = 1;
foreach( $_POST['text'] as $val )
if( $val )
$message .= "Text number: ".($index++)." contains: $valrn";
Edit: please also note that there is no need to check is $_POST[text][n]
if set, because all elements of an array are set, either if they are empty.
If your <input type="text">
have the attribute name
as text[0]
, text[1]
, etc, in your php code they values are not in $_POST[text[n]]
, but in $_POST[text][n]
.
So, you can obtain desired output in this way:
$message = '';
foreach( $_POST['text'] as $key => $val )
$message .= "Text number: ".($key+1)." contains: $valrn";
Or — if you want skip empty values — in this way:
$message = '';
$index = 1;
foreach( $_POST['text'] as $val )
if( $val )
$message .= "Text number: ".($index++)." contains: $valrn";
Edit: please also note that there is no need to check is $_POST[text][n]
if set, because all elements of an array are set, either if they are empty.
edited Feb 19 '16 at 13:54
answered Feb 19 '16 at 13:32
fusion3kfusion3k
9,76941433
9,76941433
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
add a comment |
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
Thanks this is exactly what I needed!
– remkovdm
Feb 19 '16 at 13:58
add a comment |
Here is a way to do it :
if(0 < sizeof($_POST['text']))
$message = '<ol>';
foreach($_POST['text'] as $text)
if(false === empty($text))
$message .= "<li>".$text."</li>";
$message = '</ol>';
else
$message = "void";
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
add a comment |
Here is a way to do it :
if(0 < sizeof($_POST['text']))
$message = '<ol>';
foreach($_POST['text'] as $text)
if(false === empty($text))
$message .= "<li>".$text."</li>";
$message = '</ol>';
else
$message = "void";
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
add a comment |
Here is a way to do it :
if(0 < sizeof($_POST['text']))
$message = '<ol>';
foreach($_POST['text'] as $text)
if(false === empty($text))
$message .= "<li>".$text."</li>";
$message = '</ol>';
else
$message = "void";
Here is a way to do it :
if(0 < sizeof($_POST['text']))
$message = '<ol>';
foreach($_POST['text'] as $text)
if(false === empty($text))
$message .= "<li>".$text."</li>";
$message = '</ol>';
else
$message = "void";
answered Feb 19 '16 at 13:20
ThinkTankThinkTank
1,122715
1,122715
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
add a comment |
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
That works perfect!
– remkovdm
Feb 19 '16 at 13:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35506253%2fphp-how-to-loop-through-existing-values-and-put-them-together%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Doesnt PHP return an array for
$_POST['text']
?– DanielM
Feb 19 '16 at 13:21