2

I have a HTML form with embedded PHP code that creates a checkbox for each value contained in an array. Just like this:

<?php 
  $rows = array_map( 'str_getcsv', file( 'file.csv' ) );
  $header = array_shift( $rows );
  foreach ( $rows as $row ) {
    echo '<input type="checkbox" id="'.$row[0].'" name="'.$row[0].'">
          <label for="'.$row[0].'">'.$row[0].'</label>
          <input type="number" name="'.$row[0].'" placeholder="Some text">';
  }
?>

Now, I want to send this form using this code, which is inserted into another PHP file:

<?php
  if( isset( $_POST ) == true && empty( $_POST ) == false ) {
    $account = $_POST['account'];
    $investment = $_POST['row[0]'];
    $password = $_POST['password'];
    $formcontent=" Account: $account \n $row[0]: $investment \n Password: $password";
    $recipient = "[email protected]";
    $subject = "My Form";
    $mailheader = "From: My Form <[email protected]>";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Some text";
  } 
?>

But it doesn't work. When you click on submit button the form does nothing.

I've checked it with success with HTML-only code, so I guess I'm making a mistake with PHP.

For those interested, here's a link to my form: Example

EDIT: I've removed preventDefault, as pointed by @DavidJorHpan, but I'm still stuck. I'm unable to make my form.php send $row[0] to my email.

5
  • what is the error code? Now you explain a little more? Commented Dec 13, 2017 at 23:59
  • Can you give us the error you're getting. Commented Dec 14, 2017 at 0:01
  • @ÖzgürCanKaragöz I've updated my question. Thanks for your help. Commented Dec 14, 2017 at 1:17
  • @JakeSylvestre Please see Comment #3. Commented Dec 14, 2017 at 1:19
  • 1
    You are using the name attribute wrong. Use it like this name="operative[]" then use value attribute to set the actual value Commented Dec 14, 2017 at 1:52

2 Answers 2

1

Because you use preventDefault so it will never submit form until you code for submitting form

$("button").click(function(e) {
  e.preventDefault();
});

You can remove that code or add code like $('form').submit();

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe I'm missing your point, but I didn't use preventDefault.
you used in your index.js
It works, but I'm still stuck. Please check new post edit. Thanks.
1

As David JorHpan pointed out in the second answer, you've got to remove preventDefault() from the button click event. That prevents the form from being submitted.

For every checkbox you have a corresponding number input field. Although possible, its not a good practice to have spaces in your 'name' attribute values. Try replacing those spaces with dashes or underscores. For example you can do something like below:

name="'.str_replace(' ','_',$row[0]).'"

and same can be done to id attribute values.

Your form submit check should work but it will make more sense if you change that as follows:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
   // process here
}

After doing these changes try loading the page and see how it goes.

2 Comments

It works, but I'm still stuck. Please check new post edit. Thanks.
@ÁngelNarciso Please fix your name attribute on input fields. Are you looking to get multiple values or just a single value out of those check boxes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.