Reputation: 2659
I have multiple possible users that are posted from a form to my PHP script through AJAX.
In that script I have the following code:
parse_str($_POST['users'], $useroutput);
foreach($useroutput as $key => $user){
// If both fields are filled in:
if(!empty($user['username']) OR !empty($user['password'])){
$userstring .= $user['username'].' '.$user['password'].'<br>';
}else{
echo 'Empty';
}
}
However the above loop always shows me 'Empty'.
While this is what my array looks like if I print it:
[username] => Array
(
[0] => username
[1] => anotherusername
)
[password] => Array
(
[0] => password
[1] => anotherpassword
)
)
How can I change my array to look like this?:
Array(
[0] => Array
(
[username] = myusername
[password] = mypassword
)
[1] => Array
(
[username] = anotherusername
[password] = anotherpassword
)
)
I've tried different ways of posting the data but until now nothing gives me the array as I need it.
This is how I post my form data to my PHP script:
// Add/edit users script
$( "#companywrap" ).on("click", "#saveuser", function( event ) {
// Stop normal form behaviour
event.preventDefault();
var $form = $("#userform"),
postBody = $form.serialize(),
url = $form.attr( "action" );
var posting = $.post( url, {users: postBody});
// Show result in a div
posting.done(function( data ) {
$( ".resultmessageuser" ).empty().slideDown('fast').append( data );
});
});
My form markup as requested:
<form id="userform" action="includes/userscript.php" method="post" enctype="multipart/form-data">
<div class="card m-b-20">
<div class="card-body">
<div class="form-group fieldGroup">
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
<div class="col-sm-8">
<input class="form-control" name="username[]" value="<?PHP echo $getcompany['username']; ?>" type="text" required>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
<div class="col-sm-8">
<input class="form-control" name="password[]" placeholder="<?PHP echo $editpass; ?>" value='' type="text" required>
</div>
</div>
<div class="input-group-addon">
<a href="javascript:void(0)" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Extra gebruiker</a>
</div>
</div>
<!-- copy of input fields group -->
<div class="form-group fieldGroupCopy" style="display: none;">
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
<div class="col-sm-8">
<input class="form-control" name="username[]" value="" type="text" id="example-text-input">
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
<div class="col-sm-8">
<input class="form-control" name="password[]" placeholder="<?PHP echo $editpass; ?>" value='' type="text" id="example-text-input">
</div>
</div>
<div class="input-group-addon">
<a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
</div>
</div>
</div>
</div>
</form>
Upvotes: 0
Views: 68
Reputation: 1922
You guys are overthinking this. Bad input, bad output. You are not naming the keys in the html, which results in an enumerated array instead of an associative array. You don't need to do complicated php looping tricks to account for badly laid out form markup, you just need to correct the form markup to nest the way you want.
This:
<input class="form-control" name="username[]" value="<?PHP echo $getcompany['username']; ?>" type="text" required>
Needs to be this:
<input class="form-control" name="users[]['username']" value="<?PHP echo $getcompany['username']; ?>" type="text" required>
And this:
<input class="form-control" name="password[]" placeholder="<?PHP echo $editpass; ?>" value='' type="text" required>
Needs to be this:
<input class="form-control" name="users[]['password']" placeholder="<?PHP echo $editpass; ?>" value='' type="text" required>
This will result in an array on the backend that looks like this:
print_r($_POST['users'];
array(
[0] => array(
'username' => 'some_username',
'password' => 'some_password'
)
);
Don't be clever. Keep it simple.
Upvotes: 1
Reputation: 12132
I think you should start by modifying how your HTML is rendered. Currently your form
is rendered in this manner:
<form>
<input type="text" name="username[]">
<input type="text" name="password[]">
<input type="text" name="username[]">
<input type="text" name="password[]">
<button type="submit">Submit</button>
</form>
When this is sent to PHP, the POST request would be read like this:
[
"username" => [
0 => "john"
1 => "jane"
]
"password" => [
0 => "pw1"
1 => "pw2"
]
]
However, if you change your HTML to be formatted like this:
<form>
<input type="text" name="credentials[0][username]">
<input type="text" name="credentials[0][password]">
<input type="text" name="credentials[1][password]">
<input type="text" name="credentials[1][password]">
<button type="submit">Submit</button>
</form>
then PHP, will see the POST request like this:
[
"credentials" => [
0 => [
"username" => "john"
"password" => "pw1"
]
1 => [
"username" => "jane"
"password" => "pw2"
]
]
]
Once your HTML is fixed, you would just need to make minor adjustments:
$.post( url, {users: postBody});
, to $.post( url, postBody);
Now you can use the PHP iteration you initially wanted, like so:
foreach($_POST['credentials'] as $credential){
if(!empty($credential['username']) || !empty($credential['password'])){
$userstring .= $credential['username'].' '.$credential['password'].'<br>';
}else{
echo 'Empty';
}
}
Upvotes: 3
Reputation: 23958
You are looping it incorrectly.
When you loop foreach($useroutput as $key => $user){
You get the following:
(
[0] => username
[1] => anotherusername
)
And at this point you don't have any $user['username']
you have $user[0]
and [1]
If you do like this:
foreach($useroutput['username'] as $key => $val){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
Var_dump($users);
Your $users array will work in the array and will have the users grouped as you expect.
Upvotes: 1
Reputation: 9034
If both indexes meet up then you could try:
$new_array = [];
for($i = 0; $i < count($useroutput); $i++){
$new_array[] = ['username'=>$useroutput['username'][$i], 'password'=>$useroutput['password'][$i]];
}
$new_array
would look like:
Array(
[0] => Array
(
[username] = myusername
[password] = mypassword
)
[1] => Array
(
[username] = anotherusername
[password] = anotherpassword
)
)
Upvotes: 1