ajax - PHP post method apparently not working -
latest edit: turns out that, in formprocessing.php, isset($_post['submit1']) false (where 'submit1' name of submit button; did not have name). seen in code below, did not test originally.
it explain lot. remaining question why isset($_post['submit1']) false.
note: question has been edited reflect more recent insights.
as php beginner, i’m experimenting posting forms server, , not work. in likelyhood i’m overlooking simple, don’t see it.
i have 2 files: ‘form.php’ , ‘formprocessing.php’, both located in same folder. have included full code below, first explanation. file ‘form.php’ contains form itself, including ‘post’ method. file ‘formprocessing.php’ destination, speak, of ‘post’ method, i.e. “action = formprocessing.php”.
the idea formprocessing should take place without 'formprocessing.php' loading or 'form.php' reloading (hence "event.preventdefault();" in form.php). i’m aware posting ‘form.php’ itself, want go 2 separate files.
i’ve taken code of ‘form.php’ jquery-site literally (http://api.jquery.com/jquery.post/ , last example). i’ve changed file name in action attribute ‘formprocessing.php’, , added necessary json.stringify command (see below).
the rest of code in ‘formprocessing.php’ extracts value of posted input field (there 1 field in form), , gives ‘form.php’ in form of json-object (which stringified in form.php). @ least that’s idea!
the script generated @ 'formprocessing.php.' - after typing, say, "xxx" input field - follows:
<script> {"content":"xxx"} </script>
now eyes, script seems correct -- it? first thing i'd know sure now.
edit: i've removed html tags in formprocessing.php. means generated script reduces {"content":"xxx"} , i.e. json-object. doesn't change result though.
because go wrong later. is, @ end of ‘form.php’, attach contents of data returned ‘formprocessing.php’ div element. in fact string attached div turns out "{"length":0,"prevobject":{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"length":9},"selector":"#content"}" -- i.e., indicating object of length 0. rather actual data supposedly returned 'formprocessing.php' (which said equal original input form field).
here comes code of ‘form.php’:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jquery.post demo</title> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <form action="formprocessing.php" id="searchform"> <input type="text" name="s" placeholder="search..." /> <input type="submit" value="search" /> </form> <!-- result of search rendered inside div --> <div id="result"></div> <script> /* attach submit handler form */ $("#searchform").submit(function(event) { /* stop form submitting */ event.preventdefault(); /* values elements on page: */ var $form = $( ), term = $form.find( 'input[name="s"]' ).val(), url = $form.attr( 'action' ); /* send data using post */ var posting = $.post( url, { s: term } ); /* put results in div */ posting.done(function( data ) { var content = $( data ).find( '#content' ); contentstring = json.stringify(content); $( "#result" ).empty().append( contentstring ); }); }); </script> </body> </html>
and here’s code of ‘formprocessing.php’:
<!doctype html> <html lang="nl"> <head> <meta charset="utf-8" /> <title>contact</title> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> </head> <body> <script> <?php echo "alert('hello!');"; $invoer = $_post['s']; echo ( json_encode(array("content" => $invoer)) ); ?> </script> </body> </html>
any appreciated.
your formprocessing.php file needs:
<?php $invoer = isset($_post['s']) ? $_post['s'] : 'not set'; echo '$invoer is:'.$invoer; ?>
Comments
Post a Comment