473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ajax onclick button not populating a textbox

547 512MB
I have this Ajax with PHP that determines my value correctly from a php sql query, on my "test page".

When adding the code to my main page, the button adds the correct data for a half second, and then removes it, leaving the "categories" input textbox empty.

The rest of the page is populated onload from local storage, before i press this button to populate the "categories" textbox. Any suggestions please?


Expand|Select|Wrap|Line Numbers
  1. HTML <td><input type="text" name="categories" id="categories" placeholder="categories" /></td>  
  2. (The textboxes below are populated from localstorage)   
  3.  <td><input type="text" name="gender" id="gender" placeholder="gender" /></td>  
  4. <td><input type="text" name="age" id="age" placeholder="age" /></td>  
  5. <td><input type="text" name="racecode" id="racecode" placeholder="racecode" /></td> 
  6.  
  7. <button id="but" font size="2" style ="color:red" >Add Category </button>
Ajax
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.         $(document).ready(function(){
  4.             $("#but").click(function(){
  5.             var vargender = $("#gender").val();
  6.             var varracecode = $("#racecode").val();    
  7.             var varage = $("#age").val();    
  8.         $.ajax({
  9.             method: "post",
  10.             url: "category2.php",
  11.             data: {gender:vargender,racecode:varracecode,age:varage}
  12.         })
  13.         .done(function(data){
  14.             $("#categories").val(data);
  15.  
  16.            });
  17.           });
  18.         });
  19.  
  20. </script>
Category2.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("connection.php");
  3. $gender = $_POST['gender'];
  4. $racecode = $_POST['racecode'];
  5. $age = $_POST['age'];
  6. $query = ("SELECT * FROM tblcategories WHERE '$age' >= age_from AND '$age' <= age_to AND '$racecode' = racecode AND '$gender' = gender");
  7. $result = mysqli_query($connection, $query);
  8. $output = '';
  9. while($row = mysqli_fetch_assoc($result))
  10. {
  11.  $output = $row["category_name"];
  12.  
  13. echo($output);
  14.  
  15. }
  16. ?>
Jul 6 '17 #1
1 1568
neelsfer
547 512MB
I seem to have fixed it by adding "e.preventDefault();" below button click. Hope this code helps somebody else also
Jul 6 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: KS | last post by:
I write my html pages as .jsp files (java server pages). The final result when the page is rendered in the browser, and I do a view source, is nothing less the normal HTML codes. The problem is I...
0
by: ar | last post by:
If I had a form with one textbox on it and I wanted to populate that textbox on page load, how could I do that from a global procedure? I tried creating a separate class file with the following...
5
by: middletree | last post by:
Is there a script that will populate, with a click, a textbox with the equivalent of the ASP/VBScript Now() function? In ohter words, I'd like it to be in this format: 7/25/2005 10:58:39 AM
2
by: anony | last post by:
Hi, I'm trying to dynamically add a button to my ASP.NET / VB page. I can't seem to figure out how to add a routine to the OnClick event. Any insight would be greatly appreciated! Thanks,...
7
by: Jan Hoffman | last post by:
hi @all I have the following element in an ascx-file: <asp:TextBox id="name" runat="server">name</asp:TextBox> The textbox belongs to an application of a telefonebook. I want that if I...
3
by: wardy1975 | last post by:
Hi all....looking for a bit of help here....I'm using an asp.net server side button, and trying to add an onclick event on the button to call a javascript function. However, I am trying to enter...
1
by: rn5a | last post by:
I want to create a custom control that encapsulates a Button & a TextBox. When the Button is clicked, the user is asked a question using JavaScript confirm (which shows 2 buttons - 'OK' &...
0
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am using a popup control extender (ajax control) and have placed a .net button on the panel that is displayed when the hyperlink is selected that is the target control of the popup extender. ...
1
by: akarsh | last post by:
Hi, I am using AJAX in one of my application and because of it I am getting the problem of AJAX back button. So can u tell me please that how to solve this problem.
3
by: sinducnair | last post by:
how to set imagebutton inside a textbox control in asp.net 2008?My aim is to write somethng in this text box and click on the image button inside the text box to clear the textbox.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.