#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0

    OnClick : how to pass variable from a query


    [code]
    // fills up the drop box
    $(document).ready(function(){
    $("#clients").load("ajax_get_clients_list.php");
    });

    function addClient(str1)
    {
    alert("Hello! you got into the addClient function in file ajax_update_programs_clients.php!!"+str1);
    alert(str1);
    xmlhttp.open("POST","insert_new_programs_clients.php?q3="+str1,true);
    xmlhttp.send();
    }

    <form name="MyForm" onSubmit="" method="post">
    Select Client:
    <select name="clients" id="clients" onchange=""></select> // THIS WORKS. I can send the id using the action on the form to a new page and it shows up correct. but I want to process through the function using the onclick() instead.

    <input type="submit" name="btnSubmit" id="clients" value="add client" onclick="addClient();">
    <br /></center>
    </form>
    [code]

    I need the client ID to be passed to the addClient function so this ID # can then be passed as q3 to a call to my insert file insert_new_programs_clients.php?q3=

    I feel I am making this harder then it needs to be but I can't figure this out. thx for the help or direction to help samples.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,759
    Rep Power
    9397
    Code:
    onclick="addClient();"
    That's where you call the function so that's where you need to pass the value for str1.

    Try using
    Code:
    this // the button
    .form // the form the button is in
    .clients // the client list
    .value // the value in the selected <option>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by requinix
    Code:
    onclick="addClient();"
    That's where you call the function so that's where you need to pass the value for str1.

    Try using
    Code:
    this // the button
    .form // the form the button is in
    .clients // the client list
    .value // the value in the selected <option>
    I tried those. this.form shows as undefined. this.value passes whatever string is in quotes and this.clients says [object NodeList]
  6. #4
  7. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,759
    Rep Power
    9397
  8. #5
  9. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    97
    Rep Power
    4
    Could you post the 'ajax_get_clients_list.php' code?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by richpri
    Could you post the 'ajax_get_clients_list.php' code?
    yes...

    Code:
    <?php
    if(!$_SESSION)
    {session_start();}
    error_reporting(E_ALL); 
    ?>
    
    <?php
    include_once ('db_connection.php');//Including our DB Connect 
    mysqli_select_db($dbc,"ntimetrics01");
    $sql="SELECT * FROM CLIENTS";
    $results = mysqli_query($dbc,$sql) or die(trigger_error(mysql_error(), E_USER_ERROR));
    
    while($row = mysqli_fetch_array($results)) {
    
      	  echo '<option value="'.$row['id'].'">'.htmlspecialchars($row['client_name']).'</option>'; 
      }							
    mysqli_close($dbc);
    ?>
  12. #7
  13. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    97
    Rep Power
    4
    I suggest that you use a debugger like Firebug to look at the source of the web page. Check that the load worked. It should look something like:

    Code:
    <form name="MyForm" onSubmit="" method="post">
    Select Client:
    <select name="clients" id="clients" onchange="">
    <option value="ID1">Client 1</option>
    <option value="ID2">Client 2</option>
    <option value="ID3">Client 3</option>
    <option value="ID4">Client 4</option>
    </select>
    <input type="submit" name="btnSubmit" id="clients" value="add client" onclick="addClient();">
    <br /></center>
    </form>
    then after you click on submit:


    Code:
    // selobj should be a collection of all the options in the dropdown list.
    var selobj = document.getElementById(#clients);
    // sindex should be the index of the selected option
    var sindex = selobj.options.selectedIndex;
    // selected should be the actual selected option
    var selected = selobj.options.item(sindex);
    // and svalue should be the value of that option.
    var svalue = selected.value;
    I have not tested any of this but I hope it will be of some help.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    Thanks. I just installed firebug and see if I can trace this down.
  16. #9
  17. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,054
    Rep Power
    487
    Your Javascript function is expecting a parameter yet you call it without one = error.

    Try putting a value in the caller and see if that gets passed or not ...

    example:
    <input type="submit" name="btnSubmit" id="clients" value="add client" onclick="addClient('hello world');">

    ...

    and make the initial alert in the function simpler; example:

    function addClient(str1) {
    alert(str1);


    IF you get the alert "hello world", then you can say for sure that your function's being called and the value is being passed to it.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo