How to pass dynamic data from a PHP form?

0

I'm creating a form where a field is dynamic, I'm not sure how I would pass these various data through POST. Fields that are not dynamic 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 shipping 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";
        }
    }
?>

The js to include the 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();
  });
}
    
asked by anonymous 14.04.2018 / 16:02

1 answer

0

Well, you can not use an input / select / textarea that has the same name inside the same form.

In this case you have two options:

  • Clone the form all, then each form will be sent at a time.
  • 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 name (put this below <form>

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

    Let's rename 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 cloned ones and look for the 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";
                }
            }
        }
    }
    
        
    14.04.2018 / 16:44