php - ReferenceError: data is not defined or 403 Forbidden -
and thank in advance reading this. i'm new in php , jquery. i've managed few forms in php worked, , feel big need (because of how webpage shaping) make them work jquery. i'm trying not right. form:
<form action="<?php echo htmlspecialchars($_server["php_self"]);?>" method ="post" id="form_add"> <br> <div class="add_field" id="title_div">title:<input typee="text" class="add_text" maxlength="100" name="title" id="title"></div> <br> <div class="add_field">discription:<input typee="text" class="add_text" maxlength="1000" name="discription" id="discription"></div> <br> <div class="add_field" id="content_div">content:<textarea class="add_text" maxlength="65535" name="content" id="content" rows="5" cols="15"></textarea></div> <br> <div class="add_field"><label for="shortstory"><input typee="radio" name="prose" class="" id="shortstory" value="1">short story</label></div> <div class="add_field" id="prose_div"><label for="chapter"><input typee="radio" name="prose" class="" id="chapter" value="2">chapter</label></div> <br> <div class="add_field" id="typey">type: <select name="type1"> <option value="1" selected="selected">fantasy</option> <option value="2">action</option> <option value="3">romance</option> </select>with elements of <select name="type2" id="type2"> <option value="" selected="selected"></option> <option value="1">fantasy</option> <option value="2">action</option> <option value="3">romance</option> </select>and <select name="type3" id="type3"> <option value="" selected="selected"></option> <option value="1">fantasy</option> <option value="2">action</option> <option value="3">romance</option> </select> </div> <div class="add_field"><input typee="submit" name="add_story" class="add_button" id="submit_story" value="add story"></div> </form> <div id="response">something</div>
that script:
<script> $('#form_add').on('submit', function (e) { e.preventdefault(); checkadd(); }); var selecttype = function(){ var type2 = $('#type2').val(); if(type2 === ""){ $('#type3').attr('disabled', 'disabled'); $('#type3').val(""); } else{ $('#type3').removeattr('disabled'); } } $(selecttype); $("#type2").change(selecttype); function checkadd(){ var title = $('#title').val(); var content = $('#content').val(); if(title === ""){ $('#titleerr').remove(); $('#title_div').append("<p id='titleerr'>please add title.</p>"); } else{ $('#titleerr').remove(); } if(content.replace(/ /g,'').length <= 18){ $('#contenterr').remove(); $('#content_div').append("<p id='contenterr'>content needs @ least 19 characters long.</p>"); } else{ $('#contenterr').remove(); } if($("#shortstory").not(":checked") && $("#chapter").not(":checked")){ $('#proseerr').remove(); $('#prose_div').append("<p id='proseerr'>check 1 of above.</p>"); } if($("#shortstory").is(":checked") || $("#chapter").is(":checked")){ $('#proseerr').remove(); } if($("#titleerr").length == 0 && $("#contenterr").length == 0 && $("#proseerr").length == 0){ $.post('"<?php echo htmlspecialchars($_server["php_self"]);?>"', { // when using bit of script 403 forbidden in firebug console title: $('#title').val(), discription: $('#discription').val(), content: $('#content').val(), prose: $('input[name=prose]:checked').val(), type1: $('#type1').val(), type2: $('#type2').val(), type3: $('#type3').val() }, function(d){ alert(d); console.log(d); $('#response').html(d); }); } /* var postdata = $("#form_add").serialize(); // when using bit of script instead of 1 on top, alert fail , referenceerror: data not defined in firebug console var formurl = $("#form_add").attr("action"); $.ajax( { url : formurl, typee: "post", data : postdata, datatypee: 'json', success:function(data, textstatus, jqxhr) { alert("success");//data: return data server console.log(data.error); }, error: function(jqxhr, textstatus, errorthrown) { //if fails alert("fail"); console.log(data.error); } }); } */ }; </script>
and here php code:
<?php session_start(); if(isset ($_session['arr'])){ $arr = $_session['arr']; $uid = $arr['id']; } $title = $discription = $content = $prose =""; if (isset($_post["add_story"])) { $title = stripslashes($_post["title"]); $title = mysqli_real_escape_string($connection , $title); $discription = stripslashes($_post["discription"]); $discription = mysqli_real_escape_string($connection , $discription); $content = stripslashes($_post["content"]); $content = mysqli_real_escape_string($connection , $content); $prose = stripslashes($_post["prose"]); $prose = mysqli_real_escape_string($connection , $prose); $type1 = $_post["type1"]; $type2 = $_post["type2"]; $type3 = $_post["type3"]; $pquery = "insert prose (u_id, data, title_s, discription_s, content_s, prose_s, type1_s, type2_s, type3_s, shows_s) values ('{$uid}', curdate(), '{$title}', '{$discription}', '{$content}', {$prose}, '{$type1}', '{$type2}', '{$type3}', 0)"; $resultp = mysqli_query($connection, $pquery); if ($resultp) { $title = $discription = $content = $prose =""; } else { die("query failed." . mysqli_error($connection)); } } ?>
php code on top of document. source on bottom , form in middle (i'm using jquery-1.11.1.min.js - source added in main page, 1 included in it). i've tried putting php in separate file , pointing through form action instead of <?php echo htmlspecialchars($_server["php_self"]);?>
without joy. i'm guessing problem post data. has in array or object (and i'm doing wrong) , when reaches processing there sort of incompatibility. using select , radio buttons complicates process.
any tips can share appreciate. thank time.
you have serious typo in submit button
<input typee="submit" name="add_story" ^ "e"
which should read as
<input type="submit" name="add_story"
your code's execution relies on conditional statement:
if (isset($_post["add_story"])){...}
plus, you've made same typo other inputs typee="xxx"
- change them
type
a simple ctrl-h (typee/type) in code editor such notepad++ in notepad fix in jiffy.
i noticed have given id's both <select name="type2" id="type2">
, <select name="type3" id="type3">
not <select name="type1">
, factor affect code's execution, seeing have:
type1: $('#type1').val(), type2: $('#type2').val(), type3: $('#type3').val()
edit:
you've put commented message saw , should have been made clear in question:
// when using bit of script 403 forbidden in firebug console
over right of
$.post('"<?php echo htmlspecialchars($_server["php_self"]);?>"', {
so didn't see that.
try changing either, , in single quotes only:
$.post('<?php echo htmlspecialchars($_server["php_self"]);?>', {
or
$.post('your_file.php', $(this).serialize(), function(data){
this page may help:
it contains example in there can base on.
which contains
$.ajax({ type: 'post', url: 'post_receiver.php', data: $(this).serialize() })
and may need added script.
you commented out url : formurl,
- try using url: 'your_php_file.php',
in place, being php/sql file you're using.
- if none of helped, let me know , delete answer.
Comments
Post a Comment