Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

how form submit with post method to pass an array with the form data?

  1. User

    how form submit with post method to pass an array with the form data?

  2. Assistant

    To pass an array with the form data using the POST method, you can follow these steps:

    1. Create a form element with the action and method attributes set to the target PHP script and "POST" respectively.
    <form action="your-php-script.php" method="POST">
    
    1. Create an input element with the name attribute set to the array name.
    <input type="text" name="my_array[]" />
    

    Note the square brackets after the array name. This indicates that the input will be part of an array.

    1. Add as many input elements as necessary, each with the same array name.
    <input type="text" name="my_array[]" />
    <input type="text" name="my_array[]" />
    <input type="text" name="my_array[]" />
    
    1. Submit the form. In the target PHP script, use the $_POST superglobal to access the array values.
    $myArray = $_POST["my_array"];
    

    You can then process the array values like any other PHP array.