...

View Full Version : php mysql dynamic list boxs and fields - I stuck!!



andybid
11-30-2010, 12:30 PM
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
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
//**************************************
// 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" />';

}


?>

Fumigator
11-30-2010, 09:02 PM
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:



$result = mysql_query("SELECT cost FROM resolutions.cost WHERE weeks='$drop_var'")


I think if you change that, you'll be one step closer.

andybid
11-30-2010, 09:26 PM
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

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



$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.:D:thumbsup::D
The new line $result['cost'] is now in the script at http://madandi.gotdns.com

Fumigator
11-30-2010, 09:38 PM
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.)

andybid
11-30-2010, 10:27 PM
Ah, yes I see. I made that bumfart too and was trying that on $result a number of times, now looks like


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. :confused:

Thanks for your time :thumbsup:

PS In UK and off to bed -2231 gmt - back in 11 hrs.

andybid
12-01-2010, 03:32 PM
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/dhtmltutors/ajaxgetpost.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.

andybid
12-01-2010, 11:23 PM
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!!

Fumigator
12-02-2010, 02:00 PM
Ok... Well... Not sure how I can help from here... So... Good luck! :thumbsup:

andybid
12-02-2010, 02:12 PM
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




<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==4 && 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
$Ymax=$_GET['Ymax'];
$Ymax2=$_GET['&Ymax2'];

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

andybid
12-02-2010, 02:41 PM
the other way i try is this



if(xmlhttp.readyState==4 && 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?

Fumigator
12-02-2010, 03:58 PM
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/

andybid
12-02-2010, 11:17 PM
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 (http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php)
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!

andybid
12-02-2010, 11:25 PM
it is at
http://madandi.gotdns.com/test/php_3.html

php_3..html


<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" + queryString, true);
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
$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 ;)

andybid
12-03-2010, 01:18 PM
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

seco
12-03-2010, 01:59 PM
i see your using my old chained select script. sweet.

andybid
12-03-2010, 06:33 PM
seco:

i see your using my old chained select script. sweet.
Ah thank you for that, yes, I have been learning from that, I am afraid that I cant find the link where I got it from originally now.

I have been playing around as you can see from my posts. If any cedits in it are missing let me know and I will happily add them.

I notice you highlighted oold, is this now out of date, is there a better way.?

Q. With list box select, do I need to sanitise the input?

And one more thing for you all, With this updating a total cost by adding all the costs on the page if not null, I am going to have to add this to the end of every filling the cost functions on the page arn't I, or is there another way.

PS. I have no idea how to add up the cost values into a total....any pointers please on where to start.

Once again, Thank you seco - Your code is a great help. :thumbsup:

andybid
12-03-2010, 07:04 PM
ok I add


<SCRIPT language = JavaScript>

function calculate() {
A = document.myForm.time.value
B = document.Form2.Cost2.value
A = Number(A)
B = Number(B)


C = (A + B)
document.TOTAL.TOTAL.value = C
}

</SCRIPT>


to head of script

I have added


cost :
<input type='Cost2' id='Cost2' onChange="calculate()">
to both cost value inputs on screen

I also added


<form name="TOTAL">
TOTAL COST:<INPUT NAME="TOTAL" id ="TOTAL" VALUE="">


</form>


to end of script, but it's not working!!!

I was following this http://homepage.ntlworld.com/kayseycarvey/variables4.html and am sure I done it all right.

I'll leave it on http://madandi.gotdns.com/test/php_3.html

Any feedback what I am doing wrong much appreciated..

Full script of the page is on http://madandi.gotdns.com/test/script.html <-- which has been offline for last 18 hours! DOH!

andybid
12-04-2010, 07:34 PM
ok, got the subtotals,
got the total calculation by changing the onChange function in both weeks select options to onChange="senddata(),calculate()"

however, it calculates the previous entry, not the current entry.

and sometimes totals 4.979999997 (try 1,4 and 1,2 in select box's then change one of them again to get that result cos it one behind!)

anyone know why it is calclating the previous entry. Thanks

working at
http://madandi.gotdns.com/php_3.html
current script:
http://mandandi.gotdns.com/script.html

andybid
12-09-2010, 11:52 AM
solved elsewhere

smn1111
03-24-2011, 08:06 PM
Hello,
I too am stuck.
My submit button is on the original index.php page and I'm unable to retrieve the selected boxes from any of my select boxes (I only carry about the 2nd one though, the "chained" / dependent).
Could someone help me with how to pass this value onto index.php??

tomws
03-25-2011, 03:32 AM
Hello,
I too am stuck.
My submit button is on the original index.php page and I'm unable to retrieve the selected boxes from any of my select boxes (I only carry about the 2nd one though, the "chained" / dependent).
Could someone help me with how to pass this value onto index.php??

You might have better luck starting with a new post instead of hijacking a 3-month old thread.

smn1111
03-25-2011, 04:38 PM
Will do.
I just didn't want people telling me to search the forums, saying it's been discussed, etc.
Was also hoping the author would post his resolution as we're basically using the same code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum