How to pass an array in HTML attributes and not to show single quotes?

Asked

Viewed 1,015 times

1

Following this post I created the following html by passing arrays as an input element attribute:

...
<input type="text" name="setValues[cardExpiration]" id="card_expiry" class="input-small" value="1222">
<input type="text" name="setValues[ipAddress]" id="ip" value="24.37.246.194">
...

After the form is submitted we will have the following multi-dimensional array within $_POST:

enter image description here

Understand with var_dump() the keys of the internal array:

echo '<pre>';
    var_dump($_POST);
echo '</pre>';

enter image description here

When we try to recover the values this is what happens:

$email = (isset($_POST["setValues"])?$_POST["setValues"]["email"]:FALSE);//NULL

$email = (isset($_POST["setValues"])?$_POST["setValues"]["'email'"]:FALSE);//Retorna o e-mail preenchido no formulário

The centerpoint here is these simple quotes ' key ' that is being created inside the array key.

That’s why $_POST["setValues"]["'email'"] works and $_POST["setValues"]["email"] does not work, returns NULL.

So my question is how could we create this array within the input attribute without these simple quotes appearing in the key?

1 answer

1


I tested it here and it works yes normally.

When you send a form this way:

            <div>
                    <label>Nome:</label>
                    <input type="text" name="form[nome]">
                </div>

                <div>
                    <label>E-mail:</label>
                    <input type="text" name="form[email]">
                </div>

                <div>
                    <label>Telefone/Whatsapp:</label>
                    <input type="text" name="form[telefone]">
                </div>
            </div>

php will normally receive:

var_dump($_POST["form"]["nome"]);

// irá exibir
string(87) "Quo molestias deserunt voluptas fuga Molestiae animi vel aliquip elit quam ipsum soluta"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.