Change language

How do I get $ _POST from multiple checkboxes?

Note.The name attribute of the checkboxes must have the same name and must be array-initialized. This can be done with the [] at the end of the name attribute of the checkbox.Example :
  • Code:page ( index.html) containing a form with name, email address, contact and skills as fields and message method. Note that for skills, each checkbox entry has skills []in the name attribute. < html > < head > < title > How to Get $_POST from multiple checkboxes? < / title > < / head > < body > < form action = "process.php" method = " post " > < div > < label > Name: < / label > < input type = "text" name = "name" > < / div > < div > < label > Email: < / label > < input type = "email" name = "email" > < / div > < div > < label > Skills: < / label > < input type = "checkbox" name = "skills []" value = "Java" > Java < input type = "checkbox" name = "skills [ ] " value = "Php" > PHP < input type = "checkbox" name = "skills [ ] " value = "Python" > Python < input type = "checkbox" name = "skills [ ] " value = "JavaScript" > JavaScript < / div > < div > < label > Contact: < / label > < input type = "number" name = "contact" > < / div > < div > < button type = "submit" > Submit < / button > < / div > < / form > < / body > < / html >
  • Code:page (process .php), on which we process the data submitted by the user.  
    // Get the value of the name field
    // from the $_POST array

    $name = $_ POST [ ’name’ ];  
    // Get the value of the contact field
    // from $_POST array $contact = $_ POST [ ’contact’ ];  
    // Get the value of the email field // from $_POST array $email = $_ POST [ ’email’ ];  
    // Check if at least one skill is checked if
    // checked, then assign the array returned
    // variable $_POST [’ skill ’] to $skill otherwise
    // assign an empty array $skills = (isset ( $_ POST [ ’skills’ ]))? $_ POST [ ’skills’ ]: array ();  
    ?>  

    Confirmation

    Name:   echo $name ; ?> < / p>

    Email:   echo $email ; ?> < / p>

    Contact:   echo $contact ; ?> < / p>

    Skills:
     
    // Check if there is at least one in the $skills array
    // element, if so, then iterate
    // each element and displays its value, otherwise
    // echo that no skill is selected if ( count ( $skills ) > 0) { foreach ( $skills as $skill ) { echo $skill . ’’ ; } } else { echo "No skill has been selected" ; }
    ?>
    < / p>

Output:
  • Before submitting the form ie "index.html"
  • After submitting the form, i.e. E. "Process.php"

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method