Jump to content

Sending Information From One Function To Another.


dzhax
 Share

Recommended Posts

I have these two functions:

function check_cat(){  global $prefix;  $dropdown = '<select name="category">';  $result = mysql_query('SELECT id, name FROM cat');  if(mysql_num_rows($result) < 1)  {	return false;  }    while($cat = mysql_fetch_array($result))  {	$dropdown .= '<option value="'.$cat['id'].'">'.$cat['name'].'</option>';  }  $dropdown .= '</select>';  return $dropdown;}function check_sub_cat($selcatid){  global $prefix;  $dropdown1 = '<select name="name">';  $result1 = mysql_query("SELECT id, name FROM sub_categories WHERE cat.id = '" . $selcatid . "'");  if(mysql_num_rows($result1) < 1)  {	return false;  }    while($sub_cat = mysql_fetch_array($result1))  {	$dropdown1 .= '<option value="'.$sub_cat['name'].'">'.$sub_cat['name'].'</option>';  }  $dropdown1 .= '</select>';  return $dropdown1;}

and they are called on the page like so...

<div id="images2">			  Category to Remove Forum From: '. check_cat() .'<br /><br />			</div>			<div id="images">			Name: ' . check_sub_cat($_POST['category']) . '<br /> <br />			</div>

I need to grab the value in the combo box created by check_cat() so i can make the check_sub_cat() function query only the sub categories under that main category.If this dosent make sense please let me know ill try to explain better.

Edited by dzhax
Link to comment
Share on other sites

You either need to use Javascript to update the second list, or have the first list submit the form and then get the value from $_POST.
Can i pass the php function with a onchange="check_sub_cat($_POST['category'])" event on the check_cat() function combo box?
Link to comment
Share on other sites

No, because PHP is server-side. Once you send the page to the client, the page has no knowledge of PHP. You can't tell the page to just "call check_sub_cat()", because it doesn't know what check_sub_cat() is.You will have to have a JavaScript function, check_sub_cat(), that then uses AJAX to send a request back to the server for the result of check_sub_cat() on the PHP side.

Link to comment
Share on other sites

What is the equivilant to my functions in javascript? If you can make both javascript and make them piggyback off each other that would be great thanks.like i know i can use document.writeln(); for the layout but the mysql query stops me. and then making the values show up in the box. Im very new to javascript. I learned a little today in my database class, it was a lesson with XML.And i can't ask my teacher cuz all he knows is what the book explains. and how to explain it to us. I already asked him this.

Edited by dzhax
Link to comment
Share on other sites

There are things that Javascript can't do, and that's exactly the purpose of PHP. Javascript can't read databases or write in files.There's a technology called "AJAX" that uses Javascript to receive information from an external PHP file asynchronously. This means that it can get information from PHP after a page has loaded. You will have to first learn Javascript and PHP, and then go look through the W3Schools AJAX tutorial.

Link to comment
Share on other sites

The functions above are PHP functions. If you want to use their functionality without reloading the page, you have to use AJAX. AJAX is not a specific language, it is a combination of technologies (JavaScript, the XMLHTTPRequest concept, and "XML") that allows the asynchronous execution of arbitrary server requests.

Link to comment
Share on other sites

The functions above are PHP functions. If you want to use their functionality without reloading the page, you have to use AJAX. AJAX is not a specific language, it is a combination of technologies (JavaScript, the XMLHTTPRequest concept, and "XML") that allows the asynchronous execution of arbitrary server requests.
Seriously I do not exactly understand the AJAX atm. So i was wondering if i could make java make a variable available that the next php function can read to change the result of it.like the java does a document.getElementById['category').(what ever else needs added here to make the value of category a variable)<?php check_sub_cat(THE JAVA VARIABLE HERE); ?>Function that runs for check_sub_cat()
function check_sub_cat($mainCatID){  global $prefix;  $dropdown1 = '<select name="name">';  $result1 = mysql_query("SELECT id, name FROM sub_categories WHERE cat_id = '" . $mainCatID . "'");  if(mysql_num_rows($result1) < 1)  {	return false;  }    while($sub_cat = mysql_fetch_array($result1))  {	$dropdown1 .= '<option value="'.$sub_cat['name'].'">'.$sub_cat['name'].'</option>';  }  $dropdown1 .= '</select>';  return $dropdown1;}

Sorry for trying to keep away from AJAX but I really don't want to have to learn a new language atm. Kinda busy as it is. I would like to keep the javascript and php only.

Link to comment
Share on other sites

PHP does not run after the page has loaded. PHP processes on the server, and when it's finished, it sends the information back to the client. Javascript can only interact with whatever has been sent to the client. The PHP function is on the server.The one and only way to obtain information from a PHP function after the page has loaded is with AJAX. AJAX is not a new language: it stands for Asynchronous Javascript and XML. It is just a Javascript object that has access to the server asynchronously (which means after the page has loaded).

Link to comment
Share on other sites

maybe there is a different way someone can think of how to do this.ill post the sections of my code that will be used for thisThis code is what shows up now:

case 'removeforum':	  echo '<form action="?action=config" method="post">';	  echo '	  Remove Object:<br />	  <input type="hidden" name="action" value="removeforum">	  Type: <select name="type" style="width: 158px;"  onchange="displayimages();">			<option value="1">Category</option>			<option value="2">Forum</option>			</select><br /><br /><br />			<div id="images2">';	  echo '	Category to Remove Forum From: '. check_cat() .'<br /><br />';	 	  echo '</div>			<div id="images">			Name: ' . check_sub_cat() . '<br /> <br />			</div>			<input type="submit" value="Submit" />';			break;

This is the form that the admin/moderator will use to remove categories and sub categories from the forum.The onchange="displayimages();" calls the javascript function to determine which of the divs will show. Like if you are deleting a category it will not show the other information needed to delete a sub category. This is why there is <div id="images"> and <div id="images2">This is the javascript for those functions (This can not change too much unless i rename it and add it in again. Other sections use this same javascript.):

<script>document.getElementById('images').style.display = 'none';function displayimages(){  e = document.forms[0].type.value;  if(e == 1)  {	document.getElementById('images').style.display = 'none';	document.getElementById('images2').style.display = 'block';  }  else  {	document.getElementById('images').style.display = 'block';	document.getElementById('images2').style.display = 'block';  }}</script>

These are the two functions in the functions.php that are included near the top of the page:

function check_cat($setcat){  global $prefix;  if (!$setcat == ''){	  $dropdown = '<select name="category" value="' . $setcat . '">';  } else {	  $dropdown = '<select name="category" onchange="setvar();">';  }  $result = mysql_query('SELECT id, name FROM cat');  if(mysql_num_rows($result) < 1)  {	return false;  }    while($cat = mysql_fetch_array($result))  {	$dropdown .= '<option value="'.$cat['id'].'">'.$cat['name'].'</option>';  }  $dropdown .= '</select>';  return $dropdown;}function check_sub_cat(){  global $prefix;  $dropdown1 = '<select name="name">';  $result1 = mysql_query("SELECT id, name FROM sub_categories");  if(mysql_num_rows($result1) < 1)  {	return false;  }    while($sub_cat = mysql_fetch_array($result1))  {  if($result1['cat_id'] == $_REQUEST['category'])	$dropdown1 .= '<option value="'.$sub_cat['name'].'">'.$sub_cat['name'].'</option>';  }  $dropdown1 .= '</select>';  return $dropdown1;}

Basically the function that needs changed is when the <select name='type'...> value is forum. When that is clicked (triggering the onchange="..." shows the list of existing categories in the forum.This part is what needs changed. Currently it also shows the list of all sub categories. But, I need it to only show a list of Sub Categories that fall under the selected Category. This way no errors occur. Currently as long as you pick the correct sub category it will delete them.Now that you know my situation, Can you tell me if AJAX is the only solution? If so, could someone write me what AJAX code would be needed? I can provide more details if needed.Thank you so much for all the help so far. I know I'm kind of a pain but trying to introduce a third technology into the equation more than likely will spell disaster on my part. It has happened to me before so I am trying to be cautious. Thanks for understanding and hopefully you will see where I am coming from.---------------------------

AJAX is not a new language: it stands for Asynchronous Javascript and XML.
I know its not a new language, I meant it is new to me. I only knew of its existence like 2 months ago.
Link to comment
Share on other sites

I know its not a new language, I meant it is new to me. I only knew of its existence like 2 months ago.
He meant AJAX is not a language at all...You can either use AJAX for this, or refresh the page entirely.
Link to comment
Share on other sites

Guess im gonna have to try the refresh...Is there a way to onload make a certain object in this case a combo box preselect a value?Like user selects Forum Then Category list shows up. When user selects a category, it "refreshes" and loads the page with that category still selected?

Link to comment
Share on other sites

You use the selected="selected" attribute to make an option selected by default.

<select>	<option>1</option>	<option selected="selected">2</option></select>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...