Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery AJAX custom form not returning results from PHP
    primarykey
    data
    text
    <p>I don't like the look of normal dropdown selectors in forms, so I've decided to create my own version of them with some Jquery as a little learning project for myself. </p> <p>The objective of the following code is to be able to submit the text inside the various "select" boxes, i.e. <code>#cuisineSelect</code>, <code>#locationSelect</code> and <code>#priceSelect</code> to "ajax.php" so that I can manipulate those variables further. </p> <p>My problem is that for some strange reason ajax.php will not register the $_POST variable and it's telling me that I've got an "undefined index". </p> <p>I did some debugging in IE9 with some developer tools and they show (at least I think so) that the variables are getting sent through to ajax.php, and ajax.php is returning a variable. Here are the screenshots.</p> <p><img src="https://i.stack.imgur.com/122rT.png" alt="IE9 Developer Tools Results - Just in case this helps."></p> <p>On the real project, the user can click on the <code>&lt;li&gt;</code> options item and the text will transfer onto the selector - just like a dropdown selector. </p> <p>Here is my code: </p> <p>The HTML: </p> <pre><code>&lt;div id="parentContainer" style="width:100%;"&gt; &lt;div class="container"&gt; &lt;div class="select" id="cuisineSelect"&gt; Cuisine &lt;/div&gt; &lt;div class="option" id="cuisineOption"&gt; &lt;ul id="cuisineul"&gt; &lt;li&gt;Asian&lt;/li&gt; &lt;li&gt;American&lt;/li&gt; &lt;li&gt;Indian&lt;/li&gt; &lt;li&gt;Fusion&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="container"&gt; &lt;div class="select" id="locationSelect"&gt; Location &lt;/div&gt; &lt;div class="option" id="locationOption"&gt; &lt;ul id="locationul"&gt; &lt;li&gt;Asian&lt;/li&gt; &lt;li&gt;American&lt;/li&gt; &lt;li&gt;Indian&lt;/li&gt; &lt;li&gt;Fusion&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="container"&gt; &lt;div class="select" id="priceSelect"&gt; Price &lt;/div&gt; &lt;div class="option" id="priceOption"&gt; &lt;ul id="priceul"&gt; &lt;li&gt;Price Range&lt;/li&gt; &lt;li&gt;Cheap ($5-$15)&lt;/li&gt; &lt;li&gt;Medium ($16 - $20)&lt;/li&gt; &lt;li&gt;Pricey ($21 - $35)&lt;/li&gt; &lt;li&gt;Fine Dining ($35+)&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="container"&gt; &lt;div class="select" id="searchButton" style="width:25px; height:20px;"&gt; &lt;center&gt;&lt;img align="center" src="images/sml_search.png" width="17" height="18" /&gt;&lt;/center&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="result" style="z-index:10;"&gt; result=show; &lt;?php include "ajax.php"; ?&gt; &lt;/div&gt; </code></pre> <p>The Jquery code (this has already been placed inside a $(document).ready function.) </p> <pre><code>$("#searchButton").click(function() { /*var cuisine = $("#cuisineSelect").html(); var location = $("#locationSelect").attr('value'); var price = $("#priceSelect").attr('value'); $.ajax({ type: "POST", url: "ajax.php", data: "cuisine="+ cuisine +"&amp; location="+ location +"&amp; price="+ price, success: function(){ $('#result').show(); } }); return false; */ $.post('ajax.php', 'cuisine=' + $("#cuisineSelect").text(), function () { $("#result").show(); }); }); </code></pre> <p>The PHP page "ajax.php"</p> <pre><code>&lt;?php //Search information //$cuisine = htmlspecialchars(trim($_GET['cuisine'])); //$location = htmlspecialchars(trim($_POST['location'])); //$price = htmlspecialchars(trim($_POST['price'])); //echo $location; //echo $price; //$addClient = "INSERT INTO clients (fname,lname) VALUES ('$fname','$lname')"; //mysql_query($addClient) or die(mysql_error()); //$value = $_POST['cuisine']; //$value2 = $_POST['val']; //echo "$value2"; $cuisine = $_POST['cuisine']; echo $cuisine; ?&gt; </code></pre> <p>Thanks very much for your help. </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.
    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