Pass option multiple with jQuery Chosen plugin to PHP form -
i have html form. have jquery animation (dialog popup , validation) sends data form.php
.
my problem: email name, email , phone, 1 option passed. can't multiple choices...
can me please?
so here code:
html
<form class="form_class" id="form_id" method="post" name="contactform" action="form.php"> <label for="nom">name</label> <br/> <input name="nom" type="text" id="nom" /><br/> <label for="email">email</label> <br/> <input name="email" type="text" id="email" /><br/> <label for="tel">phone</label> <br/> <input name="tel" type="text" id="tel" /><br/> <select multiple="multiple" name="beats[]" data-placeholder="choose beats" id="beats" style="width:300px;" tabindex="4"> <option name="alternatefunkydelicbeat" value="alternatefunkydelicbeat"> alternatefunkydelicbeat</option> <option name="bottom" value="bottom"> bottom</option> <option name="free_speech" value="free_speech"> free speech</option> <option name="fuck_friends" value="fuck_friends"> fuck friends</option> <option name="hopeless_streets" value="hopeless_streets"> hopeless streets</option> <option name="if_i_die_tonight" value="if_i_die_tonight"> if die tonight</option> <option name="infamous" value="infamous"> infamous</option> <option name="obvious_behavior" value="obvious_behavior"> obvious behavior</option> <option name="off_the_flight" value="off_the_flight"> off flight</option> <option name="pissed" value="pissed"> pissed</option> </select> <input type="image" src="images/button.png" id="button" width="100" height="56" target="_parent"/></form>
here jquery @ top of html page:
$(document).ready(function() {
$('#form_id').submit(function(){ nom = $(this).find("#nom").val(); email = $(this).find("#email").val(); tel = $(this).find("#tel").val(); /*beats = $(this).find("#beats").val();*/ beats = $(this).find("#beats").val(); $.post('form.php',{ nom:nom, email:email, tel:tel, beats:beats }, function(data){ if(data.error =='mail_invalide'){ $('#basic-modal-content2').modal(); return false; }else if(data.error =='vide'){ $('#basic-modal-content1').modal(); return false; }else{ $('#basic-modal-content3').modal({onclose: function(){ $("input").val(''); $("textarea").val(''); $.modal.close(); }}); return false; } }, "json"); return false; }); }); </script>
and finally, form.php
<?php $e = array(); $e['error'] = "non valid form"; if(empty($_post['nom']) || empty($_post['tel'])){ $e['error'] = "vide"; } elseif(!filter_var($_post['email'], filter_validate_email)){ $e['error'] = "mail_invalide"; } else{ $e['error'] = 'ok'; $nom = $_post['nom']; $email = $_post['email']; $tel = $_post['tel']; $beats = $_post['beats']; $to = 'w-productions@hotmail.com'; $sujet = 'new message w-productions.com '.$email; $body = "new message w-productions.com \n\n name: $nom \n emali: $email \n phone: $tel \n\n beats: $beats"; mail($to, $sujet, $body); } echo json_encode($e); ?>
$_post['beats']
array of selected options.
if need list of selected options, try :
$beats = implode(',', $_post['beats']);
on html side, dont need set name attribut option element. can simplify part : nom = $(this).find("#nom").val();
nom = $("#nom").val();
using ids.
Comments
Post a Comment