mulaus
07-27-2013, 12:50 PM
hi
i need some help i have this code that will display data when user fill in in the input value using jquery
how do i get the data of the if else result
let say if i key in 100 i want to put 'High' in in the mysql database
echo "<tr class='odd'> <script src='http://code.jquery.com/jquery-1.9.1.js'></script>
<input type='text' value=''/>
<p></p>
<script>
$('input').keyup(function () {
var value = $(this).val();
if (value=='')
{
}
else if (value >= 50) {
$('p').text('High');
}
else {
$('p').text('Low');
}
}).keyup();
</script>";
jscheuer1
07-27-2013, 01:44 PM
I don't know database code, but you would create a separate PHP page that will put that value into the database and do a jQuery.ajax call to that page, passing as either GET or POST the value that you want put into the database. Let's do GET for now (to do POST just change GET to POST and in the javascript change 'get' to 'post'). So, on this separate PHP page, call it - say, setdata.php, we would have:
<?php
$value = isset($_GET['value'])? $_GET['value'] : '';
//code here to put $value into the database, can test for $value and if $value === '', then either do nothing, delete the record, whatever
//just what you do with $value is up to you, it will be whatever was passed in from the 'top' page, or '' if nothing was passed
?>
Then for your script on the 'top' page:
<script>
$('input').keyup(function () {
var value = $(this).val();
if (value == '' || isNaN(value)) {
value = '';
}
else if (value > 49) {
$('p').text((value = 'High'));
}
else {
$('p').text((value = 'Low'));
}
$.ajax({
url: 'setdata.php',
cache: false,
data: {value: value},
type: 'get'
});
}).keyup();
</script>
Oh, and I was just looking at this again and wondering what the second .keyup() is for. That does nothing, so could be removed.
Also. with keyup you are checking each time a key goes up. So if they do 100 it's Low, Low, High. Usually change is used instead of keyup. However, change requires that focus be removed from the element. But you can just put a 'Go' button in there right after the text input. It doesn't have to do anything. Just clicking on it will trigger the change event:
<script>
$('input:text').change(function () {
var value = $(this).val();
if (value == '' || isNaN(value)) {
value = '';
}
else if (val . . .
and:
<input type='text' value=''/> <input type='button' value='Go'/>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.