Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy php mysql dynamic list boxs and fields - I stuck!!

    Hi

    I am trying to streamline my signup process.

    Where I am.

    user selects no of services 1-5
    this is passed to a select.php dynamically which includes 1res.php if input=1 2res.php if input=2 etc.
    This loads a page with no of fields to be completed
    first drop box = months (dynamic load from db)
    second box = weeks (dynamic load from db where = months input)
    third box = cost (dynamic but not what wanted)

    what I need

    the first and second boxs work ok

    when the user selects a value in the second box, I want to return a cost value by running a SELECT cost FROM resolutions.cost WHERE months='months input' AND weeks = 'weeks input'

    this has to return a value for each row of inputs, ie 1 res.php has 1 row, 2.res.php= 2 row etc.

    also when the cost fields are updated, if all rows cost have a value ( so not "") a total cost field is returned at the bottom of the page.

    I am stuck where I am now. I have tried various versions of code and changing things over the last 4 days and can not get the result I need. I am doing something wrong. In order to save my marriage with the time I spendin on this, I need some help.

    For some reason I cant even get the sql query to return the cost based on weeks value and months value?????!!!!!

    Can anyone help me sort this.

    To keep things simple, I have included the code for one field(1res.php)

    you can see the code below at http://madandi.gotdns.com

    Thanks

    1res.php
    PHP Code:
    <?php 
      
    include('connections/resdb.php');
      include(
    'func.php');
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>User input 1 field (1res.php)</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function() {
        $('#wait_1').hide();
        $('#drop_1').change(function(){
          $('#wait_1').show();
          $('#result_1').hide();
          $.get("func.php", {
            func: "drop_1",
            drop_var: $('#drop_1').val()
          }, function(response){
            $('#result_1').fadeOut();
            setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
          });
            return false;
        });
    });

    function finishAjax(id, response) {
      $('#wait_1').hide();
      $('#'+id).html(unescape(response));
      $('#'+id).fadeIn();
    }
    function finishAjax_tier_three(id, response) {
      $('#wait_2').hide();
      $('#'+id).html(unescape(response));
      $('#'+id).fadeIn();
    }
    </script>
    </head>

    <body>
    <p>
    <form id="FormRes1" action="" method="post">
      
        <select name="drop_1" id="drop_1">
        
          <option value="" selected="selected" disabled="disabled">Select a Category</option>
          
          <?php getTierOne(); ?>
        
        </select> 
        
        <span id="wait_1" style="display: none;">
        <img alt="Please Wait" src="ajax-loader.gif"/>
        </span>
        <span id="result_1" style="display: none;"></span>
        <span id="wait_2" style="display: none;">
        <img alt="Please Wait" src="ajax-loader.gif"/>
        </span>
        <span id="result_2" style="display: none;"></span> 
      
    </form>
    </p>
    <p>
    <?php if(isset($_POST['submit'])){
        
    $drop $_POST['drop_1'];
        
    $drop_2 $_POST['drop_2'];
        
    $drop_3 $_POST['drop_3'];
        
        echo 
    "You selected a ";
        echo 
    $drop_3." ".$drop." ".$drop_2;
    }

    $result mysql_query("select cost from resolutions.cost where months='$drop' and weeks='$drop_2'") or die (mysql_error());

     while(
    $fee_1 mysql_fetch_array$result ))
     
     echo 
    $fee_1."----    $fee_1 value     ---".$result."--    $result value    --".$drop_3." ---    drop_3 value"

    ?>
    </body>
    </html>

    func.php
    PHP Code:
    <?php
    //**************************************
    //     Page load dropdown results     //
    //**************************************
    function getTierOne()
    {
        
    $result mysql_query("SELECT DISTINCT months FROM resolutions.cost"
        or die(
    mysql_error());

          while(
    $tier mysql_fetch_array$result )) 
      
            {
               echo 
    '<option value="'.$tier['months'].'">'.$tier['months'].'</option>';
            }

    }

    //**************************************
    //     First selection results     //
    //**************************************
    if($_GET['func'] == "drop_1" && isset($_GET['func'])) { 
       
    drop_1($_GET['drop_var']); 
    }

    function 
    drop_1($drop_var)
    {  
        include_once(
    'connections/resdb.php');
        
    $result mysql_query("SELECT weeks FROM resolutions.cost WHERE months='$drop_var'"
        or die(
    mysql_error());
        
        echo 
    '<select name="drop_2" id="drop_2">
              <option value=" " disabled="disabled" selected="selected">Choose one</option>'
    ;

               while(
    $drop_2 mysql_fetch_array$result )) 
                {
                  echo 
    '<option value="'.$drop_2['weeks'].'">'.$drop_2['weeks'].'</option>';
                }
        
        echo 
    '</select>';
        echo 
    "<script type=\"text/javascript\">
    $('#wait_2').hide();
        $('#drop_2').change(function(){
          $('#wait_2').show();
          $('#result_2').hide();
          $.get(\"func.php\", {
            func: \"drop_2\",
            drop_var: $('#drop_2').val()
                  }, function(response){
            $('#result_2').fadeOut();
            setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
          });
            return false;
        });
    </script>"
    ;
    }

    //**************************************
    //     Second selection results     //
    //**************************************
    if($_GET['func'] == "drop_2" && isset($_GET['func'])) { 
       
    drop_2($_GET['drop_var']); 
    }

    function 
    drop_2($drop_var)
    {  
        include_once(
    'connections/resdb.php');
        
    $result mysql_query("SELECT cost FROM resolutions.cost WHERE weeks='$drop_var'"
        or die(
    mysql_error());
        
        echo 
    '<select name="drop_3" id="drop_3">
              <option value=" " disabled="disabled" selected="selected">Choose one</option>'
    ;

               while(
    $drop_3 mysql_fetch_array$result )) 
                {
                  echo 
    '<option value="'.$drop_3['cost'].'">'.$drop_3['cost'].'</option>';
                }
    echo 
    '</select>';
        echo 
    "<script type=\"text/javascript\">
    $('#wait_3').hide();
        $('#drop_3').change(function(){
          $('#wait_3').show();
          $('#result_3').hide();
          $.get(\"func.php\", {
            func: \"drop_3\",months: $('#drop_1')
            drop_var: $('#drop_3').val()
          }, function(response){
            $('#result_3').fadeOut();
            setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
          });
            return false;
        });
    </script>"
    ;
     echo 
    '<input type="submit" name="submit" value="Submit" />';

    }


    ?>
    Last edited by andybid; 11-30-2010 at 12:47 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    For some reason I cant even get the sql query to return the cost based on weeks value and months value?????!!!!!
    This is because you are echoing $result, not $result['cost'].

    when the user selects a value in the second box, I want to return a cost value by running a SELECT cost FROM resolutions.cost WHERE months='months input' AND weeks = 'weeks input'
    Your "drop_2" query is only filtering based on weeks, not months:

    PHP Code:
        $result mysql_query("SELECT cost FROM resolutions.cost WHERE weeks='$drop_var'"
    I think if you change that, you'll be one step closer.

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Tried, failed..

    Quote:
    For some reason I cant even get the sql query to return the cost based on weeks value and months value?????!!!!!
    This is because you are echoing $result, not $result['cost'].

    OK, I changed it to
    PHP Code:
    $result mysql_query("select cost from resolutions.cost where months='$drop' and weeks='$drop_2'") or die (mysql_error());

     while(
    $fee_1 mysql_fetch_array$result ))
     
     echo 
    $fee_1."----    fee_1 value     -- :   ".$result['cost']."    --    result value    --   : ".$drop_3."     ---    drop_3 value"
    Still no change.

    Your "drop_2" query is only filtering based on weeks, not months:


    PHP Code:
        $result mysql_query("SELECT cost FROM resolutions.cost WHERE weeks='$drop_var'"
    The problem I have is that I can't echo the months value for the 3rd box to do a 'select where AND' query.

    The script I have only holds the selected value from the box in use.

    I want to strip out the 3rd select list box and from the second , when a value is selected, display the cost from the 'select where AND' and also calculate the total cost by summing all the cost values and display at the bottom of the page.

    Thanks for your input.
    The new line $result['cost'] is now in the script at http://madandi.gotdns.com
    Last edited by andybid; 11-30-2010 at 09:28 PM.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yech I meant to say you need to echo $fee_1['cost']. Bit of a brain fart there.

    As for sending the value of the first select box when the second is changed, why can't you do that? You're already adding to your Javascript dynamically, just add that value as either a variable or just stick it in as an additional argument to the finishAjax_tier_three() function.

    Is this a situation where you are using a script you didn't write so you don't really know what it's doing? (Not that there anything wrong with that; I just need to know if you know what I'm talking about when I refer to code in the script.)

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Cobbled together copied scripts...errm...yes

    Ah, yes I see. I made that bumfart too and was trying that on $result a number of times, now looks like

    PHP Code:
     echo "   VARIABLE CONTENTS:= ".$fee_1['cost']."----    fee_1 value     -- :   ".$result."    --    result value    --   : ".$drop_3."     ---    drop_3 value"
    and works.

    Yes I am using a few copied scripts, I have changed it extensively to use here and in this context.

    I have not passed variables before and have tried various attempts here but all failed, as I said, 3 days ( and one overnighter) it was getting annoying.

    I try and follow scripts I use but AJAX is a new one on me. Javascript I only got the basics of as well.

    I tried so many things and was going in circles. I googled quite a bit but lost it somewhere. I couldn't grasp dynamically sending 2 or more variables and getting them.


    I am happy to learn and try to when on a learning cliff as much as this, If you want to talk me through the javascript as in what each line does and give hints to get me there - but I do the work, am happy to learn it. If you want to post the answer, then happy too but I won't 'get it'(as in understand) if I cant 'get it' now.

    Personally I am quite impressed that I have got as far as I have for novice.

    Thanks for your time

    PS In UK and off to bed -2231 gmt - back in 11 hrs.
    Last edited by andybid; 11-30-2010 at 10:31 PM.

  • #6
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Sorry page didn't work - trying new ideas - working again now

    No replies today, mind you for those that visited http://madandi.gotdns.com would have found that it didnt work. I spent a few hours trying to put an ajax get on the second box sdo that when the second box is selected it posts both the weeks and months value, alas tried much, didnt get there I was trying to follow http://www.javascriptkit.com/dhtmltu...xgetpost.shtml
    but couldnt get any action when selectd an item for the second box.

    See I don't sit about at least, I do try and experiment.

    Anyway, page is working with original script again so back to square 1, a place I seem to constanly be in this last week.

  • #7
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    am I dense?

    Dont answer.

    I copied the scripts on that $get link tutorial above and could not get it to work, the boxs worked fine but couldnt get the name and age value bit to display on the page once submitted. I going mad. A box of chocs to anyone that can help me get my site working properly!!! ARRGGHH!!

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Ok... Well... Not sure how I can help from here... So... Good luck!

  • #9
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thought Id go to basics

    ok, gone to basics and gone through a few walkthroughs

    this is where I got to, my probmlem now, I cant get two strings to pass to php_1.php again, If I can get this to work I can move forward. Where am I going wrong now...
    http://madandi.gotdns.com/test/php_2.php

    php_2.php


    PHP Code:
    <html>
    <
    script type="text/javascript">
    function 
    sendData(){
        if(
    window.XMLHttpRequest)
        {
    xmlhttp=new XMLHttpRequest();
        }else{
            
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            
    xmlhttp.onreadystatechange=function(){
        if(
    xmlhttp.readyState==&& xmlhttp.status==200){
        var 
    Ymax=document.getElementById('Ymax').value;
        var 
    Ymax2=document.getElementById('Ymax2').value;
            
    result=xmlhttp.responseText;
        
    //in the html element <div id="forResult"></div>//will be writen what you write with "echo" in your php file
        //in this example, "Thank you for submiting Ymax"
        
    document.getElementById('forResult').innerHTML=result;
        }
        }
        
    //this next line I had to put the document.getelement bit in otherwise all I'd get was OBJECT in  the echo where Ymax  value 
        // should be. before it was just xmlhttp.open("GET","php_1.php?Ymax="+Ymax,true
        
    xmlhttp.open("GET","php_1.php?Ymax="+document.getElementById('Ymax').value+"&Ymax2"+document.getElementById('Ymax2').value,true);
        
    xmlhttp.send();
        }
        
    </script>
        
        <body>Ymax: <input type="text" id="Ymax" size="15">
        <label for="Ymax2">Ymax2</label>
        <select name="Ymax2" id="Ymax2" onChange="sendData()">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
        </select>
        <br>
        <input type="button" value="Submit" onClick="sendData()">
        <div id="forResult"></div></body></html> 
    php_1.php


    PHP Code:
    <?php
    $Ymax
    =$_GET['Ymax'];
    $Ymax2=$_GET['&Ymax2'];

    echo 
    "Thank you for submiting  ".$Ymax."=Ymax& ".$Ymax2."=Ymax2";
    ?>

  • #10
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Should this work?

    the other way i try is this

    PHP Code:
    if(xmlhttp.readyState==&& xmlhttp.status==200){
        var 
    Ymax=document.getElementById('Ymax').value;
        var 
    Ymax2=document.getElementById('Ymax2').value;
        var 
    passstring="?Ymax="+Ymax+"&Ymax2="+Ymax2;
        
            
    result=xmlhttp.responseText;
        
    //in the html element <div id="forResult"></div>//will be writen what you write with "echo" in your php file
        //in this example, "Thank you for submiting Ymax"
        
    document.getElementById('forResult').innerHTML=result;
        }
        }
        
    //this next line I had to put the document.getelement bit in otherwise all I'd get was OBJECT in  the echo where Ymax  value 
        // should be. before it was just xmlhttp.open("GET","php_1.php?Ymax="+Ymax,true
        
    xmlhttp.open("GET","php_1.php"+passstring,true);
        
    xmlhttp.send(); 
    but all i get is an error that passstring is undefined.

    what am I doing wrong? As far as I can tell from spending the last two days on google, all this should work. I seem to have problems in the passing of 2 or more variables with ajax.]


    Fumigator, thanks for help earlier - have I upset you?

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    No, no, not upset, don't worry...

    I liked it better when you were using jQuery to do your AJAX calls. If you want to stick with jQuery, your code will be much simpler. In a couple of hours I'll have time to post a simple example, or you can check out jQuery's documentation to get started.

    http://api.jquery.com/jQuery.post/

  • #12
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Fumigator, much appreciated.

    I am having a look at that link but I cant even get the example to work, I get the error '$ is undefined'. I am doing something wrong. wood for trees.

    I am losin it.

    I need to get a page that has an entry for months (1-12) and weeks (1,2,4), I can just use a select list for this and dont really need the dynamic filling of the lists. What Id like is the weeks selection to trigger returning a cost value from a query (where = months and weeks)

    I also need to make pages for 2 of these inputs, 3 4 and 5 respectively.

    With the 2-5 input pages I need to get a Toal Cost to dynamically update adding up all the cost inputs on that page, so on the 5 input page it would add cost1 and cost2 etc and then minus a % (ammount depends on the page)

    I also need to have a seperate title box for each one but I can do that later when I submit the page and write to db.

    Fot now I just need to get this cost bit sorted, well I really need to get this posting and returning thing sorted. I don't kniow why but all the samples I have tried, if I get them to work they dont when I start changing them.

    I got this one working
    but when I duplicated it for a second line, when the 2nd week box was selected it filled the value with the value in the first cost box. (Changed Ymax to months and Ymax2 to months and deleted the sex iinput from sample)

    Anyway, I dont know whats going on. AAARRRRGGGHHHHHH!

  • #13
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it is at
    http://madandi.gotdns.com/test/php_3.html

    php_3..html
    PHP Code:
    <html>
    <
    script langumonths="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function sendData(){
        var 
    ajaxRequest;  // The variable that makes Ajax possible!
        
        
    try{
            
    // Opera 8.0+, Firefox, Safari
            
    ajaxRequest = new XMLHttpRequest();
        } catch (
    e){
            
    // Internet Explorer Browsers
            
    try{
                
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (
    e) {
                try{
                    
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (
    e){
                    
    // Something went wrong
                    
    alert("Your browser broke!");
                    return 
    false;
                }
            }
        }
        
    // Create a function that will receive data sent from the server
        
    ajaxRequest.onreadystatechange = function(){
            if(
    ajaxRequest.readyState == 4){
                
    document.myForm.time.value ajaxRequest.responseText;
            }
        }
        var 
    months document.getElementById('months').value;
        var 
    weeks document.getElementById('weeks').value;
        
        var 
    queryString "?months=" months "&weeks=" weeks ;
        
    ajaxRequest.open("GET""php_1.php" queryStringtrue);
        
    ajaxRequest.send(null); 
    var 
    cost document.getElementById('time').value
    }

    //-->
    </script>


        
        <form name='myForm'>
    Months:
      <select name="months" id="months">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option>10</option>
    </select>
      <br />
    Weeks: 
     <select name="weeks" id="weeks" onChange="sendData()">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="4">4</option>
    </select>
    cost :
    <input type='time' id='time'/>

    </form>
    </body>
    </html> 
    php_1.php
    (where the get goes)
    PHP Code:
    <?php
    $months
    =$_GET['months'];
    $weeks=$_GET['weeks'];

    include(
    'connections/resdb.php');

    $result mysql_query("SELECT cost FROM resolutions.cost WHERE months =".$months." and weeks=".$weeks."")
    or die (
    mysql_error());

    while(
    $cost mysql_fetch_array$result )) 
                {
                 echo 
    'COST'.$cost['cost'];
                             }
                
    ?>
    Halfway there
    Last edited by andybid; 12-02-2010 at 11:35 PM.

  • #14
    New Coder
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hurray, got there http://madandi.gotdns.com/test/php_3.html

    All i need now is a total cost to calculate when the individual costs are updated..... Might need some help there

  • #15
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    i see your using my old chained select script. sweet.

  • Users who have thanked seco for this post:

    andybid (12-03-2010)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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