Jump to content

pass a var from 1 function to another


tryingtolearn

Recommended Posts

Cant seem to figure this one out for a chained script.

 

In the code below I need to take the value of  $drop_var from function drop_1()

and send it to function drop_2()

So I can add it to the Where clause to limit the results of the third box a little more.

 

But I cannot get the value to pass to the function.

Any help appreciated.

func.php

<?php
//**************************************
//     Page load dropdown results     //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT branch FROM rank") 
or die(mysql_error());

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

}

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

function drop_1($drop_var)
{  

include_once('db.php');
$result = mysql_query("SELECT DISTINCT type FROM rank WHERE branch='$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['type'].'">'.$drop_2['type'].'</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_var2: $('#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_var2']);
   
}

function drop_2($drop_var2)
{  


echo "<br>($drop_var)<br>"; 
    

include_once('db.php');
$result = mysql_query("SELECT * FROM rank WHERE type='$drop_var2'") 
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['grade'].'">'.$drop_3['grade'].'</option>';
		}

echo '</select>';
    echo '<input type="submit" name="submit" value="Submit" />';
}
?>

index.php

<?php 
  include('db.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>Chained Select Boxes using PHP, MySQL and jQuery</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 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;
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/224551-pass-a-var-from-1-function-to-another/
Share on other sites

Hope this makes some sense.

 

<?php
function drop_1($drop_var){
    /**
     * all code here before starting to echo
     **/
     
    $var = 'anything you are going to echo';
    $var .= 'add some more stuff to the variable!';
    $var .= 'one more add is fun!';
    
    return $var; # instead of using echo return the variable, so then you could do something like (look below)
}

echo drop_1($_GET['drop_var']); # this will echo $var

function drop_2($drop_var){
    /**
     * wherever you want to use the output from drop_1 you would just do
     **/
    echo drop_1($drop_var);
    
    # then whatever you want to echo afterward
}
?>

Well it sort of makes sense but the problem is when I try to implement it it echos another drop box not a value

So Im not too sure whats going on.

 

Unless Im missing something that you meant.

I read about the functions variables scope etc... so its making sense but not working.

 

<?php
//**************************************
//     Page load dropdown results     //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT branch FROM rank") 
or die(mysql_error());

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

}

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

function drop_1($drop_var)
{  
    include_once('db.php');
$result = mysql_query("SELECT DISTINCT type FROM rank WHERE branch='$drop_var'") 
or die(mysql_error());
$branch=$drop_var;
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['type'].'">'.$drop_2['type'].'</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>";
return $branch;
}


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

function drop_2($drop_var)
{  
    include_once('db.php');
$result = mysql_query("SELECT * FROM rank WHERE type='$drop_var'") 
or die(mysql_error());
echo drop_1($drop_var);
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['grade'].'">'.$drop_3['grade'].'</option>';
		}

echo '</select> ';
    echo '<input type="submit" name="submit" value="Submit" />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.