javascript - AJAX form refreshes page -
have been searching around while , can't find proper answer problem. have "finance" tracker has several hidden divs using jquery appear when button show div clicked. have asset tracker queries database , upon being checked updates database new values in adjacent input rows checkbox located. trying checkbox submit data without causing page reload , div "toggle" off again.
on form section attempted remove method='post'
instead when checking box reloaded page , added post variables url string. removed action='fbook.php'
in attempt prevent reloading, did not resolve problem.
here related code php file:
if(isset($_post['assetsetupdate'])) { $assetlastupdate = $dtnowdate->format('y-m-d'); foreach($_post['assetid'] $key => $id) { if(isset($_post['assetsetupdate'][$key])) { $stmt_atrackup -> bindparam(':update', $assetlastupdate, pdo::param_str, 10); $stmt_atrackup -> bindparam(':upnotes', $_post['assetnotes'][$key], pdo::param_str, 50); $stmt_atrackup -> bindparam(':upthis', $_post['assetdescription'][$key], pdo::param_str, 50); $stmt_atrackup -> bindparam(':upval', $_post['assetvalue'][$key], pdo::param_int, 3); $stmt_atrackup -> execute(); } else continue; } echo "<div class='notice'>" . $pagetitle . " / assets updated!</div>"; }
(other code)
$(document).ready(function() { $("#assetupdateform").submit(function(e) { e.preventdefault(); $.ajax({ type: 'post', url: 'fbook.php', data: $(this).serialize(), success: function() { alert('form submitted'); }, }); return false; }); $('#shfbasset').click(function() { $('#fbasset').toggle('slow'); });
(other code)
});
(other code)
$assetcounter = 1; $stmt_atrack -> execute(); while ($row_atrack = $stmt_atrack -> fetch(pdo::fetch_assoc)) { $chage = $row_atrack['checked']; $cchage = ''; switch(true) { case (strtotime($chage) >= strtotime('-7 days')): $cchage = 'fbcan'; break; case (strtotime($chage) >= strtotime('-30 days')): $cchage = 'fbca1'; break; case (strtotime($chage) >= strtotime('-90 days')): $cchage = 'fbca3'; break; default: $cchage = 'fbca6'; break; } if(isset($row_atrack['serial'])) { $ifdserial = "<strong>serial: </strong>" . $row_atrack['serial'] . "<br/>"; } else { $ifdserial = ''; } if(isset($row_atrack['upc'])) { $ifdupc = "<strong>upc: </strong>" . $row_atrack['upc'] . "<br/>"; } else { $ifdupc = ''; } if(isset($row_atrack['related'])) { $ifdrelated = "<strong>related: </strong>" . $row_atrack['related'] . "<br/>"; } else { $ifdrelated = ''; } if(isset($row_atrack['location'])) { $ifdlocation = "<strong>location: </strong>" . $row_atrack['location'] . "<br/>"; } else { $ifdlocation = ''; } if(isset($row_atrack['tagphoto'])) { $ifdtagphoto = "<strong>tag photo: </strong><a href='images/inv/" . $row_atrack['tagphoto'] . ".jpg'>" . $row_atrack['tagphoto'] . "</a><br/>"; } else { $ifdtagphoto = ''; } $ifdetails = "<div class='upop'><img class='th_icon' src='images/icons/ic_lst.jpeg'><div class='upopo'>" . ($ifdserial) . ($ifdupc) . ($ifdrelated) . ($ifdlocation) . ($ifdtagphoto) . "</div></div>"; if($ifdetails == "<div class='upop'><img class='th_icon' src='images/icons/ic_lst.jpeg'><div class='upopo'></div></div>") { $ifdetails = ''; } echo "<form id='assetupdateform[" . $assetcounter . "]' method='post'>"; echo "<tr><input type='hidden' name='assetid[" . $assetcounter . "]' value='" . $assetcounter . "' />"; echo "<td><input type='hidden' name='assetdescription[" . $assetcounter . "]' value='" . $row_atrack['description'] . "' /><a href='https://www.google.com/#q=ebay+" . $row_atrack['description'] . "' target='_new_assetsearch'>" . $row_atrack['description'] . "</a></td>"; echo "<td>" . $row_atrack['type'] . " - " . $row_atrack['category'] . "</td>"; echo "<td><input type='checkbox' name='assetsetupdate[" . $assetcounter . "]' value='now' onchange='this.form.submit();' /></td>"; echo "<td><input type='number' name='assetvalue[" . $assetcounter . "]' value = '" . $row_atrack['value'] . "' style='width: 75px;' /></td>"; echo "<td class='" . $cchage . "'>" . $chage . "</td>"; echo "<td><input type='text' name='assetnotes[" . $assetcounter . "]' value = '" . $row_atrack['notes'] . "' style='width: 140px;' /></td>"; echo "<td>" . $ifdetails . "</td>"; echo "</tr>"; echo "</form>"; $assetcounter++; }
your javascript:
$("#assetupdateform")
… working on form id="assetupdateform"
.
your php says:
id='assetupdateform[" . $assetcounter . "]'
which in html terms becomes:
id='assetupdateform[something]'
even if variable empty, still have square brackets, id not going match , javascript event handler never bound element.
you need either use real id or, since appears can change, add class
form , use .class-selector
in javascript.
nb: html invalid. particular problem have has potential cause same symptoms describe just fixing problem above might not enough. use a validator , write valid html too.
Comments
Post a Comment