Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP post method apparently not working
    primarykey
    data
    text
    <p>LATEST EDIT: It turns out that, in formprocessing.php, isset($_POST['submit1']) is FALSE (where 'submit1' is now the name of the submit button; originally it did not have a name). As seen in the code below, I did not test for this originally. </p> <p>It does explain a lot. But the remaining question is <em>why</em> isset($_POST['submit1']) is FALSE. </p> <p>NOTE: This question has been edited to reflect more recent insights.</p> <p>As a PHP beginner, I’m experimenting with posting forms to the server, and it does not work. In all likelyhood I’m overlooking something simple, but I just don’t see it.</p> <p>I have two files: ‘form.php’ and ‘formprocessing.php’, both located in the same folder. I have included the full code below, but first some explanation. The file ‘form.php’ contains the form itself, including the ‘post’ method. The file ‘formprocessing.php’ is the destination, so to speak, of the ‘post’ method, i.e. “action = formprocessing.php”.</p> <p>The idea is that the formprocessing should take place without 'formprocessing.php' loading or 'form.php' reloading (hence "event.preventDefault();" in form.php). I’m aware that I could also be posting to ‘form.php’ itself, but for now I want to go with two separate files.</p> <p>I’ve taken the code of ‘form.php’ from the jQuery-site almost literally (<a href="http://api.jquery.com/jQuery.post/" rel="nofollow">http://api.jquery.com/jQuery.post/</a> , the last example). I’ve changed the file name in the action attribute to ‘formprocessing.php’, and added a necessary JSON.stringify command (see below).</p> <p>The rest of the code in ‘formprocessing.php’ simply extracts the value of the posted input field (there is only one field in the form), and gives it back to ‘form.php’ in the form of a JSON-object (which is then stringified in form.php). At least that’s the idea! </p> <p>The script generated at 'formprocessing.php.' - after typing, say, "xxx" into the input field - is as follows:</p> <pre><code>&lt;script&gt; {"content":"xxx"} &lt;/script&gt; </code></pre> <p>Now to my eyes, this script SEEMS correct -- but is it? That is the first thing I'd like to know for sure now.</p> <p>EDIT: I've now removed all the HTML tags in formprocessing.php. Which also means that the generated script reduces to {"content":"xxx"} , i.e. only the JSON-object. Doesn't change the result though.</p> <p>Because something does go wrong later. That is, at the end of ‘form.php’, I attach the contents of the data returned from ‘formprocessing.php’ to a div element. But in fact the string attached to the div turns out to be "{"length":0,"prevObject":{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"length":9},"selector":"#content"}" -- i.e., indicating an object of length 0. Rather than the actual data supposedly returned from 'formprocessing.php' (which as I said also equal the original input to the form field).</p> <p>Here comes the code of ‘form.php’:</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;jQuery.post demo&lt;/title&gt; &lt;script src="http://code.jquery.com/jquery-1.9.1.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="formprocessing.php" id="searchForm"&gt; &lt;input type="text" name="s" placeholder="Search..." /&gt; &lt;input type="submit" value="Search" /&gt; &lt;/form&gt; &lt;!-- the result of the search will be rendered inside this div --&gt; &lt;div id="result"&gt;&lt;/div&gt; &lt;script&gt; /* attach a submit handler to the form */ $("#searchForm").submit(function(event) { /* stop form from submitting normally */ event.preventDefault(); /* get some values from elements on the page: */ var $form = $( this ), term = $form.find( 'input[name="s"]' ).val(), url = $form.attr( 'action' ); /* Send the data using post */ var posting = $.post( url, { s: term } ); /* Put the results in a div */ posting.done(function( data ) { var content = $( data ).find( '#content' ); contentstring = JSON.stringify(content); $( "#result" ).empty().append( contentstring ); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And here’s the code of ‘formprocessing.php’:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="nl"&gt; &lt;head&gt; &lt;meta charset="UTF-8" /&gt; &lt;title&gt;Contact&lt;/title&gt; &lt;script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; &lt;?php echo "alert('Hello!');"; $invoer = $_POST['s']; echo ( json_encode(array("content" =&gt; $invoer)) ); ?&gt; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any help much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload