Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a form using ajax, onchange event, reload to SAME page
    primarykey
    data
    text
    <p>I've been studying this for a while and I'm not sure if I'm going about this the right way because every form I setup according to examples, it doesn't do what I need.</p> <p>I need to setup a form that will:</p> <ul> <li><p>set session when you select from dropdown menu</p></li> <li><p>not reload/refresh page (i've read that using AJAX solves this)</p></li> <li><p>submit and stay on SAME page (confused because most AJAX examples send it to different process.php page which is supposedly "invisible" but it doesn't "stay" on the same page, it redirects.</p></li> </ul> <p>Basically, client selects quantity of 1 to 10. If they select "2"... it does NOT reload the page.. but it DOES set a session[quantity]=2. Should be simple... but do I POST to same page as form? or POST to different page and it somehow redirects? </p> <p>Also, one test I did it kept pasting my "echo session[quantity]" down the page like 7, 2, 3, 5, etc. etc. each time instead of replacing it.</p> <p>The code below is partly from an example which had gender as a dropdown menu. I just want the dropdown to STAY as whatever was selected and also echo the selection next to the submit button. This is for testing purposes so that I understand what is going on. Once that happens I can do the rest of the form. it currently won't echo anything.</p> <pre><code>&lt;? ob_start();?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;?php session_start(); ?&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;Submit Form with out refreshing page Tutorial&lt;/title&gt; &lt;!-- JavaScript --&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" &gt; $(function() { $(".submit").click(function() { var gender = $("#gender").val(); var dataString = 'gender=' + gender; if(gender=='') { $('.success').fadeOut(200).hide(); $('.error').fadeOut(200).show(); } else { $.ajax({ type: "POST", url: "join.php", data: dataString, success: function(){ $('.success').fadeIn(200).show(); $('.error').fadeOut(200).hide(); } }); } return false; }); }); &lt;/script&gt; &lt;style type="text/css"&gt; body{ } .error{ color:#d12f19; font-size:12px; } .success{ color:#006600; font-size:12px; } &lt;/style&gt; &lt;/head&gt; &lt;body id="public"&gt; &lt;div style="height:30px"&gt;&lt;/div&gt; &lt;div id="container"&gt; &lt;div style="height:30px"&gt;&lt;/div&gt; &lt;form method="post" name="form"&gt; &lt;select id="gender" name="gender"&gt; &lt;option value=""&gt;Gender&lt;/option&gt; &lt;option value="male"&gt;Male&lt;/option&gt; &lt;option value="female"&gt;Female&lt;/option&gt; &lt;/select&gt; &lt;div&gt; &lt;input type="submit" value="Submit" class="submit"/&gt; &lt;span class="error" style="display:none"&gt; Please Enter Valid Data&lt;/span&gt; &lt;span class="success" style="display:none"&gt; Your gender is &lt;?php echo $_SESSION['gender'];?&gt;&lt;/span&gt; &lt;/div&gt; &lt;/form&gt; &lt;div style="height:20px"&gt;&lt;/div&gt; &lt;/div&gt;&lt;!--container--&gt; &lt;/body&gt; &lt;/html&gt; &lt;? ob_flush(); ?&gt; </code></pre> <p>and here is my page where the POST goes called join.php (called that in example so I went with it for now)</p> <pre><code>&lt;?php session_start(); if($_POST) { $gender = $_POST['gender']; $_SESSION['gender'] = $gender; } else { } ?&gt; </code></pre>
    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.
    1. This table or related slice is empty.
    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