Awais Umar
Awais Umar

Reputation: 2075

Html Checkboxes Are Not Giving Values To PHP

I am creating an html form that contains multiple checkboxes... Here is my code

Form

<form enctype="multipart/form-data" action="processstart.php" method="post" class="wpcf7-form">
  <div class="form-row">
    <span class="label--input-group">Services you require</span>
    <span class="wpcf7-form-control-wrap services">
      <span class="wpcf7-form-control wpcf7-checkbox check-group-input">
        <span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Branding" />&nbsp;<span class="wpcf7-list-item-label">Branding</span>
          </label>
        </span>
        <span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Design" />&nbsp;<span class="wpcf7-list-item-label">Design</span>
          </label>
        </span>
        <span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Other" />&nbsp;<span class="wpcf7-list-item-label">Other</span>
          </label>
        </span>
        <span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Development" />&nbsp;<span class="wpcf7-list-item-label">Development</span>
          </label>
        </span>
        <span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Illustration" />&nbsp;<span class="wpcf7-list-item-label">Illustration</span>
          </label>
        </span>
      </span>
    </span>
  </div>
</form>

Here is processstart.php

<?php
  if(isset($_POST['services'])) {
    foreach($_POST['services'] as $services) {
      echo $services;
    }
  }
?>

Now nothing is getting echoed...I have tried var_dump(get_defined_vars()); and every other variable is defined except these checkboxes. Not even showing "null" there. What is going wrong?

Upvotes: 0

Views: 140

Answers (3)

user2118784
user2118784

Reputation: 452


actually you are not submitting your form. You just specified in 'action'. To achieve your code workable, add a submit button. So that, while clicking it will call processstart.php

<input type="submit">

Upvotes: 0

Krish R
Krish R

Reputation: 22711

You have missed to close <form> tag and add submit button

 <input type="submit" name="submit" value="submit">
  </form>

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try i have action on same file but you can change action values will be appear after any checkbox is checked else you can't get checkbox values

On your form not any close <form> tag and not submit form action like button or submit button

<?php if(isset($_POST['services']))
{
    foreach($_POST['services'] as $services)
    {
        echo $services;
    }
}?>

<html>
<body>
<form enctype="multipart/form-data" action="" method="post" class="wpcf7-form">
<div class="form-row"><span class="label--input-group">Services you require</span><span class="wpcf7-form-control-wrap services"><span class="wpcf7-form-control wpcf7-checkbox check-group-input"><span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Branding" />
            &nbsp;<span class="wpcf7-list-item-label">Branding</span></label>
          </span><span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Design" />
            &nbsp;<span class="wpcf7-list-item-label">Design</span></label>
          </span><span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Other" />
            &nbsp;<span class="wpcf7-list-item-label">Other</span></label>
          </span><span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Development" />
            &nbsp;<span class="wpcf7-list-item-label">Development</span></label>
          </span><span class="wpcf7-list-item">
          <label>
            <input type="checkbox" name="services[]" value="Illustration" />
            &nbsp;<span class="wpcf7-list-item-label">Illustration</span></label>
          </span></span></span></div>
          <input type="submit" name="sub">
          </form>
</body>
</html>

Upvotes: 0

Related Questions