Passing Jquery Var To Php/Mysql Query - php

i want to pass a jquery variable into php,
l'ets say:
i have this input:
<input type="text" name="" id="course" />
an jquery:
$.get('test.php',
function(data) {
$('#details').html(data);
}
);
and test.php:
$sql = "select name, id, parent_id from mytable where parent_id='theinput'";
so basiclly i want the input data that filled by user to be in the php query ('theinput')

use this:
$.get("test.php",
{"param":"param value"},
function(returned_data)
{
alert(returned_data);
});
in the text.php
get the value of "param" and write the mysql query.

<input type="text" name="course" id="course" />
<script>
var data = $('#course').val();
$.get('test.php?data='+data);
<script>
Make the the name of the textbox that same as the id.
Take the value of the textbox with a variable which takes data from
the elements val.
You could write it like this:
$('#course').focusout(
function ()
{
var data= $(this).attr('value');
load('test.php?data='+data);
}
);

Related

How to use ajax to check unique ID in api and return data based on the unique ID

I am trying to set up a form that allows users to input an id that they've been given. When the ID is submitted, the back-end is checked to see if that id exists and if it does it returns data
I haven't really tried anything I'm just trying to wrap my mind around how to get this done
Here is the form code:
<form action="/action_page.php">
Dealership:<br>
<input type="text" name="dealership">
<br>
Email:<br>
<input type="text" name="email">
<br><br>
Phone Number:<br>
<input type="text" name="phonenum">
<br><br>
Order ID:<br>
<input type="text" name="order_id">
<br><br>
<input type="submit">
</form>
Here is the ajax:
$(document).ready(function (){
$("#works").hide();
var $dealership = $('dealership');
var $email = $('email');
var $phonenum = $('email');
var $order_id = $('order_id');
$('.submit').click (funciton(){
var track = {
dealership : dealership.val(),
email: email.val(),
phonenum = phonenum.val(),
order_id = order_id.val(),
};
$.ajax({
type: 'GET',
url: 'http://testingapi.com/api/order-status',
data: track,
success: function() {
tracking.append();
},
sucess: funciton(itWorked) {
}
});
});
});
It may be necessary to remove the submit type button and add the button type.
Also, in your code the ID is by class $ (". Submit").
then add the class to the button too.
<button type="button" class="submit">Submit</button>
This is fairly simple. Once you've accepted the order_id from the user you can pass it along in your data dictionary like this (along with anything else you want to pass):
data: {
'id': order_id,
}
On your server side you could so something like this to retrieve the ID:
In PHP:
$id = $_GET['id'];
In Django:
id = request.GET.get('id')
Once you've done this you can just query your database and pass this id in the query to check if records match or not.

How to get data value of one input field from a list?

How can I get the value of the first input field from a list?
<button id="hello">get data</button><br><br>
<table> <td><label for="voucher_<?php echo $counter; ?>">D/N Number <?php echo $counter; ?></label><br>
<input type="text" name="voucher_<?php echo $counter; ?>" class="input-field exsmall-input" value="<?php echo $voucher; ?>" placeholder="D/N Number" id="voucher_<?php echo $counter; ?>" required>
</td></table>
When I run the above code the number of rows varies depending on the counter.If I want to get the value of the first DN number ,what shall I do ?
$(document).ready(function(){
$('#hello').click(function(){
var name = $("#voucher").val();
alert(name);
});
});
When I run this javascript I get undefined vales.
The reason you are getting the error is because you have used wrong selector to target input element. input element do not have id voucher. You need to put the ID to make existing code work.
Or, You can use attribute starts with selector here from current HTML:
$('#hello').click(function(){
var name = $("input[name^='voucher_']").val();
alert(name);
});
You can do it like below:-
$(document).ready(function(){
$('#hello').click(function(){
var name = $("div[id^='voucher']:visible:first").val();
alert(name);
});
});
Or:-
$(document).ready(function(){
$('#hello').click(function(){
var name = $("input[type=text]:visible:first").val();
alert(name);
});
});
Try this:-
$(document).ready(function() {
$('#hello').click(function() {
var name = $('table').find('input[type=text]').filter(':visible:first')
.val();
});
});

Retrieve and print data from mysql DB using ajax

I have the following text fields -
<input type="text" name="id" id="id" tabindex="1">
<input type="text" name="name" id="name" tabindex="1"/>
and the screenshot of mysql database is -
id name
123 John
124 Rahi
When I insert the track id in the first text field then the corresponding student name should be shown in the second field. How can it be done using ajax ?
Try this:
>> Trigger onblur event (javascript function) for the first textbox.
>> Define ajax in that function to get name.
>> Show the name in textbox.
I think this is simplest way to get this.
--
Thanks
You can call a jquery function on onblur() event of the textbox.
<input type="text" name="id" id="id" tabindex="1" onblur="getname()">
Write the ajax function for getting the name like:
<script>
function getname()
{
var id=$("#id").val(); // get the id from textbox
$.ajax({
type:"post",
dataType:"text",
data:"id="+id,
url:"page.php", // url of php page where you are writing the query
success:function(response)
{
$("#name").val(response); // setting the result from page as value of name field
}
});
}
</script>
and write the code for fetching the result in a page, say page.php
$id=$_POST['id'];
$query=mysql_query("select name from table where id=$id");
$result=mysql_fetch_row($query);
echo $result[0]; // echo the name to js function
exit;

jQuery send post variable using ajax and submit form

I have html form with dynamical number of fields, for example:
<form id="myform">
<input type="text" id="input1">
<input type="text" id="input2">
<input type="text" id="input3">
...
<input type="text" id="inputN">
<span id="button_click"> CLICK </span>
</form>
and jQuery which is:
$("#button_click").click(function(){
$.post("myfile.php",
{
XXXX:YYYY
},
function(data,status){
// do anyting
});
});
I don't know the exact number of fields, so I can't fill XXXX - post variable name, and YYYY - field data from web page.... so I can't count/write one by one...
How can I submit whole form, through post variables, using AJAX and click button?
Sounds like you're looking for the .serialize() method:
$.post("myfile.php",
$("#myform").serialize(),
function(data,status){
// do anyting
});
http://api.jquery.com/serialize/
//rough code for general puporse of storing values for post
var obj = $("#myform"),
data = {};//to hold the values
obj.find('[name]').each(function(index, value) {
name = obj.attr('name'),
value = obj.val();
data[name] = value;
});
$.post("myfile.php",
data: data,
function(data,status){
// do anyting
});

Jquery Ajax based search

I want to do the search using ajax jquery js, for that I have taken this piece of code from here:-
Now i have some issues, I have this Javascript code:-
<script language="JavaScript" type="text/javascript">
<!--
function searchuser(cv) {
$("#SearchResult").html('<img src="loader.gif"/>').show();
var url = "elements/search-user.php";
$.post(url, {contentVar: cv} ,function(data) {
$("#SearchResult").html(data).show();
});
}
//-->
</script>
My Form:-
<label>
<input onClick="generatenew();" type="radio" name="search_option" value="code" id="search_option_0">
Customer Code</label></td>
<td><label>
<input onClick="generatenew();" type="radio" name="search_option" value="company" id="search_option_1">
Customer Company</label></td>
<td><label>
<input onClick="generatenew();" type="radio" name="search_option" value="name" id="search_option_2">
Customer Name</label></td>
<td><label>
<input onClick="generatenew();" type="radio" name="search_option" value="email" id="search_option_3">
Customer Email</label>
This is my search textbox
<input type="text" name="searchuser_text" id="newInput" size="25" maxlength="25" class="inputbox MarginTop10">
This is my search button
<input onclick="javascript:searchuser('con1');" class="Button" name="search_user_submit" type="button" value="Search">
This is my Display Area :-
<div id="SearchResult">My default content for this page element when the page initially loads</div>
Now i want to know on click of button i am sending a data i.e. con1. I want to send two data to the searchuser function, one the selected option button value and the another one is the text in the textbox. After sending both the data to the function how will i get the data in the function? Do i need to change the function searchuser(cv) to function searchuser(cv, cvtwo).
Also while the $.post(url, {contentVar: cv} ,function(data) is sending only one data to the php file, how can i send both the data i.e. cv and cvtwo to the php file?
First of all you can modify the search function to something like this
$("input[name='search_user_submit']").click(function(){
var cv = $('#newInput').val();
var cvtwo = //similar to above
var data = 'cv='+ cv + '&cvtwo='+cvtwo; // sending two variables
$("#myDiv").html('<img src="loader.gif"/>').show();
var url = "elements/search-user.php";
$.post(url, {contentVar: data} ,function(data) {
$("#SearchResult").html(data).show();
});
});
Don't use inline functions, you can do it easily with jQuery's native bind functionality:
// Search when you click on submit
$(document).on('click', '.submit_button', function(){
search('click_button');
});
// Search when you press enter
$(document).on('keypress', "#searchString", function(e){
var c = (e.keyCode ? e.keyCode : e.which);
if(c == 13) {
search('pressed_enter');
}
});
You can therefore collect the button value and pass it through to your search function:
function search(button_value) {
$("#myDiv").html('<img src="loader.gif"/>').show();
$.post("elements/search-user.php", { search_value: $('#newInput').val(), button_value: button_value} ,function(data) {
$("#SearchResult").html(data).show();
});
}
What you're doing is send off the form with two $_POST variables: one is the search string you've inputted in to the search box (called search_value) and the other is the button value.

Categories