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();
});
}