javascript - Ajax Request Hit or Miss with PHP SQL Submit -
i have form, after validation, executes 2 ajax requests , submits form create entry in mysql database. 2 ajax requests return urls (links) google documents created upload.php.
however, ajax request "agenda" comes across error when form filled data meets unknown criteria.
when same url accessed manually, url (link) of google document returned.
so ultimate problem when ajax request comes across error, cannot submit form.
after form validation, ajax requests sent upload.php , form submitted via post create.php.
the responses ajax assigned hidden values can stored along form submit.
form-validation.js
submithandler: function(form) { $('#submitbutton').button('loading'); console.log('loading button'); $("#cancelbutton").prop("disabled", true); console.log('disable button'); success1.show(); error1.hide(); var ename = $('#create_event').find('input[name="eventname"]').val(); var edate = $('#create_event').find('input[name="eventdate"]').val(); var etype = $('#create_event').find('select[name="eventtype"]').val(); var separator = "&"; var agendaurl = "http://xxxx.xxx/libraries/upload.php?ftype=agnd" + separator + "ename=" + ename + separator + "edate=" + edate + separator + "etype=" + etype; var minurl = "http://xxxx.xxx/libraries/upload.php?ftype=min" + separator + "ename=" + ename + separator + "edate=" + edate + separator + "etype=" + etype; var agendareturn = getagenda(agendaurl); console.log('called getagenda'); agendareturn.success(function(data) { $('input[name="agendaurl"]').val(data); console.log('assigned agendaurl hidden'); }); var minreturn = getmin(minurl); console.log('called getmin'); minreturn.success(function(data) { $('input[name="minurl"]').val(data); console.log('assigned minurl hidden'); submitform(); console.log('form submit called'); }); function submitform() { console.log('form submitting'); $('#create_event').unbind().submit(); } function getagenda(agendaurl) { return $.ajax({ url: agendaurl, type: 'get', datatype: 'text', async: true, }); } function getmin(minurl) { return $.ajax({ url: minurl, type: 'get', datatype: 'text', async: true, }); } }
create.php
include '../../libraries/library.php'; session_start(); if((!empty($_post)) && $_post['agendaurl'] != null) { /** * create event in main database */ $query_paramsce = array( ':eventid' => getrandom(), ':ccode' => getchapcode(), ':name' => $_post['eventname'], ':date' => $_post['eventdate'], ':timestart' => $_post['eventstart'], ':timeend' => $_post['eventend'], ':location' => $_post['eventloc'], ':type' => $_post['eventtype'], ':rank' => seteventrank($_post['eventtype']), ':description' => $_post['eventdescription'], ':agendalink' => $_post['agendaurl'], ':minuteslink' => $_post['minurl'], ':creator' => getfirstlast(), ':creatortimestamp' => timestamp() ); $queryce = " insert `events` ( eventid, ccode, name, date, timestart, timeend, type, rank, location, description, agendalink, minuteslink, creator, creatortimestamp ) values ( :eventid, :ccode, :name, :date, :timestart, :timeend, :type, :rank, :location, :description, :agendalink, :minuteslink, :creator, :creatortimestamp ) on duplicate key update eventid = :eventid "; $createevent = dbsubmit($createevent, $queryce, $query_paramsce); redirect('http://xxxx.xxx/leadership/attendance/events'); } else { echo "error. please contact adminstrator."; }
i've attached screenshot of console , webpage:
the following screenshot form data fails ajax request:
it not entirely clear question is, problem cause agenda
request fail, not encoding values use in url.
so first thing do, encode values correctly:
var agendaurl = "http://xxxx.xxx/libraries/upload.php?ftype=agnd" + separator + "ename=" + encodeuricomponent(ename) + separator + ... // etc. ^^^^^^^^^^^^^^^^^^^^^^^^^ here
you can have jquery automatically sending data
value consists of key - value pairs key name want send.
there no need make synchronous ajax calls: can start both @ same time asynchronously , submit form when both done:
$.when(agendareturn, minreturn).then(function(data1, data2) { // assign values ... // submit form ... });
lastly, seem making simple requests before submit form. these necessary? can imagine lot faster if submitted form , had server fetch urls when handle submission.
then need use urlencode()
in php encode values way.
Comments
Post a Comment