php - jQuery Ajax not submitting data -
i decided have first go ajax submit data i'm having problems. form seems reset no data sent database, i'm missing obvious here:
jquery (currently included in header of file)
<script> // jquery submit form data via ajax add recovery pack $.ajax({ type:'post', url: 'addrpack.php', data:$('#rpack_add_form').serialize(), success: function(response) { $('#rpack_add_form').find('.form_result').html(response); }} </script>
the php/html form
<form id="rpack_add_form" class='small_form' name='rpack_form' method='post' onsubmit="return submitform();"> contract: <select id="contract_select" name="contract" onchange="showcontract(this)"> <option value='0'>select contract</option> <?php $sth = $conn->query("select * `contracts`"); while($row = $sth->fetch(pdo::fetch_assoc)) { echo '<option value='.$row['contracts_id'].'>'.$row['contracts_name'].'</option>'; } ?> </select> <div id="contract1" class="admin_box"> prefix: <input name='prefix' type='text' id='prefix'><br /> number: <input name='number' type='text' id='number'><br /> suffix: <input name='suffix' type='text' id='suffix'><br /> </div> <div id="contract2" class="admin_box"> <p>sapce 2nd form @ later date</p> </div> <div id="contract3" class="admin_box"> <p>sapce 3rd form @ later date</p> </div> received: <select id="select_receive" name="received" onchange="showlocation(this)"> <option value="0">no</option> <option value="1">yes</option> </select><br /> <div id="location_box" style="display: none; padding-top: 5px;">location: <input name='location' type='text' id='location'></div> <input class='button' type=submit value='add recovery pack' name='add_rpack'> </form> <div class="form_result"> </div> <a class='hide_div' href='javascript:void(0);' onclick='hiderdiscdiv()'>close</a>
the php addrpack.php (this code works when remove ajax part above , submit normal)
<?php session_start(); include_once 'config.php'; include_once 'connect.php'; $prefix = $_post['prefix']; $number = $_post['number']; $suffix = $_post['suffix']; $contract = $_post['contract']; $received = $_post['received']; $location = $_post['location']; //check if number has been entered if (empty ($number)) { echo "you need enter number"; }else { $sth = "insert `rpacks` (rpacks_prefix, rpacks_number, rpacks_suffix, rpacks_contract, rpacks_receive, rpacks_location) values (:prefix, :number, :suffix, :contract, :received, :location)"; $q = $conn->prepare($sth); $q->execute(array(':prefix'=>$prefix,':number'=>$number,':suffix'=>$suffix,':contract'=>$contract, ':received'=>$received, ':location'=>$location)); echo "added"; }
probably syntax error - missing );
$('#rpack_add_form').submit(function(){ $.ajax({ type:'post', url: 'addrpack.php', data:$('#rpack_add_form').serialize(), success: function(response) { $('#rpack_add_form').find('.form_result').html(response); } }); return false; });
Comments
Post a Comment