How to pass dynamic data from a PHP form?

Asked

Viewed 165 times

0

I am creating a form where a field is dynamic, I do not know how to pass this various data through the POST. Fields that are not dynamic I can send.

The form:

              <form action="files/includes/contact/contato-orcamento.php" method="post" class="contact-form wpcf7-form">
                <div class="wprt-contact-form-1"> <span class="wpcf7-form-control-wrap name">
                  <input type="text" tabindex="1" id="name" name="name" value="" class="wpcf7-form-control" placeholder="Nome *" required>
                  </span> <span class="wpcf7-form-control-wrap email">
                  <input type="email" tabindex="2" id="email" name="email" value="" class="wpcf7-form-control" placeholder="E-mail *" required>
                  </span> <span class="wpcf7-form-control-wrap phone">
                  <input type="text" tabindex="3" id="phone" name="phone" value="" class="wpcf7-form-control" placeholder="Telefone ">
                  </span>
                  <span class="wpcf7-form-control-wrap produto col-md-12"  style="padding: 0px;">
                  <div class="form-group dynamic-element" style="display:none">                         
                    <div class="col-md-8">
                    <span class="wpcf7-form-control-wrap">
                      <select id="item" name="item[]" class="wpcf7-form-control">
                        <option value="0" selected="selected">Selecione</option>
                        <!--{loop:i}--> 
                        <!--{<option value="1">i.item_nome</option>}--> 
                        <!--{end:i}-->
                      </select>
                      </span>
                    </div>
                    <div class="col-md-3"> <span class="wpcf7-form-control-wrap">
                      <input type="text" id="quantidade" name="quantidade" class="wpcf7-form-control" placeholder="Quantidade" />
                      </span> </div>                       

                    <!-- End of fields-->
                    <div class="col-md-1 ">
                      <p class="delete">x</p>
                    </div>
                  </div>
                  <fieldset>
                    <!-- Form Name -->
                    <div class="dynamic-stuff col-md-12"> 
                      <!-- Dynamic element will be cloned here --> 
                      <!-- You can call clone function once if you want it to show it a first element--> 
                    </div>

                    <!-- Button -->
                    <div class="col-md-4"></div>
                    <div class="form-group col-md-4 adicionar-produto">
                      <div class="row">
                        <p class="add-one">Adicionar produto</p>
                      </div>
                    </div>
                    <div class="col-md-4"></div>
                  </fieldset>
                  </span>
                  <span class="wpcf7-form-control-wrap message">
                  <textarea name="message" tabindex="5" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea" placeholder="Mensagem" required></textarea>
                  </span>
                  <div class="wrap-submit">
                    <input type="submit" value="ENVIAR" class="submit wpcf7-form-control wpcf7-submit" id="submit" name="submit">
                  </div>
                </div>
              </form>

The sending code:

<?php
define( "WEBMASTER_EMAIL", '' );

$error = false;
$fields = array( 'name', 'email', 'phone', 'message' );

foreach ( $fields as $field ) {
    if ( empty( $_POST[$field] ) || trim( $_POST[$field] ) == '' ){
        $error = true;
    }
}

if ( ! $error ) {
    $name = stripslashes( $_POST['name'] );
    $email = trim( $_POST['email'] );
    $subject = stripslashes( $_POST['phone'] );
    $subject = stripslashes( $_POST['item'] );
    $subject = stripslashes( $_POST['quantidade'] );
    $message = stripslashes( $_POST['message'] );

    $mail = @mail( WEBMASTER_EMAIL, $subject, $message,
         "From: " . $name . " <" . $email . ">\r\n"
        ."Reply-To: " . $email . "\r\n"
        ."X-Mailer: PHP/" . phpversion() );

    if ( $mail ) {
        header("Location: /contatosucesso");
    } else {
            echo "Error";
        }
    }
?>

js to include dynamic fields:

$('.add-one').click(function(){
  $('.dynamic-element').first().clone().appendTo('.dynamic-stuff').show();
  attach_delete();
});


//Attach functionality to delete buttons
function attach_delete(){
  $('.delete').off();
  $('.delete').click(function(){
    console.log("click");
    $(this).closest('.form-group').remove();
  });
}
  • You’re not getting it because you’re overwriting inputs. The ideal is to rename or use them quantidade[], for example.

1 answer

0


Well, you cannot use an input/select/textarea that has the same name within the same form.

In this case you have two options:

  1. Clone the whole form, then each form will be sent at a time.
  2. Clone the div, but change the name of all inputs to a dynamic name

I’ll put option 2 here:

Let’s create a clone counter to use in the name (put this below the <form>

<input type="hidden" value="0" name="count" id="count" />

We will rename the inputs at the time of cloning

$('.add-one').click(function(){
   var count = parseInt($("#count").val());

   var div = $('.dynamic-element').first().clone()
   div.find("input, select").each(function(index, obj){
        $(obj).attr("name", $(obj).attr("name") + "-"+count;
   });
   div.appendTo('.dynamic-stuff').show();
   count++;
   $("#count").val(count);
   attach_delete();
});

Now in PHP, take the number of clones made and look for inputs:

$numClonagem = intval($_POST["count"]);

for($i=0;  $i<$numClonagem; $i++){
    $fields = array( 'name-'.$i, 'email-'.$i, 'phone-'.$i, 'message-'.$i );
    $error = false;

    foreach ( $fields as $field ) {
        if ( empty( $_POST[$field] ) || trim( $_POST[$field] ) == '' ){
            $error = true;
        }
    }

    if ( ! $error ) {

        $name = stripslashes( $_POST['name-'.$i] );
        $email = trim( $_POST['email-'.$i] );
        $subject = stripslashes( $_POST['phone-'.$i] );
        $subject = stripslashes( $_POST['item-'.$i] );
        $subject = stripslashes( $_POST['quantidade-'.$i] );
        $message = stripslashes( $_POST['message-'.$i] );

        $mail = @mail( WEBMASTER_EMAIL, $subject, $message,
             "From: " . $name . " <" . $email . ">\r\n"
            ."Reply-To: " . $email . "\r\n"
            ."X-Mailer: PHP/" . phpversion() );

        if ( $mail ) {
            header("Location: /contatosucesso");
        } else {
                echo "Error";
            }
        }
    }
}

Browser other questions tagged

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