Results 1 to 2 of 2

Thread: get jquery if else result

  1. #1
    Join Date
    Sep 2009
    Posts
    48
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question get jquery if else result

    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

    PHP Code:
    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>"


  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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 Code:
    <?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:

    Code:
    <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:

    Code:
    <script> 
    $('input:text').change(function () { 
    var value = $(this).val(); 
    
    if (value == '' || isNaN(value)) { 
     value = '';
    } 
    
    else if (val . . .
    and:

    Code:
    <input type='text' value=''/> <input type='button' value='Go'/>
    Last edited by jscheuer1; 07-27-2013 at 02:47 PM. Reason: javascript code improvement, later add observations
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 3
    Last Post: 12-18-2012, 06:56 AM
  2. Resolved jQuery assign innerHtml to result of ajax
    By keyboard in forum JavaScript
    Replies: 2
    Last Post: 04-02-2012, 11:35 AM
  3. Resolved Joining result of multiple selects to get final result?
    By gwmbox in forum JavaScript
    Replies: 2
    Last Post: 01-04-2011, 01:18 PM
  4. Change this to get this result
    By chrjoh88 in forum PHP
    Replies: 2
    Last Post: 12-03-2010, 09:45 AM
  5. Resolved echo result from db
    By john0611 in forum PHP
    Replies: 2
    Last Post: 08-10-2009, 12:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •