|
-
Sep 8th, 2005, 12:18 AM
#1
Thread Starter
Addicted Member
function.. [RESOLVED]
i am trying to create a function.. but i don't know how to use it.. a procedure(as in VB) but how?
I don't want it to be in a different file...
PHP Code:
<html>
<head>sample</head>
<body>
*********** how will i use dbconnect in here **********
</body>
</html>
<?php function dbconnect($host,$user,$password)
$conn = mysql_connect($host, $user, $password)
or die ("Could Not Connect to DB");
?>
a sample of function and procedure and how to use it will be excellent.. sorry just started PHP yesterday.. hope you guys can help me..
Last edited by titan7262; Sep 12th, 2005 at 02:16 AM.
-
Sep 8th, 2005, 01:55 AM
#2
Re: function..
My experience is limited with PHP but shouldn't it be like this:
PHP Code:
Code:
<html>
<head>sample
<?php function dbconnect($host,$user,$password) ;
$conn = mysql_connect($host, $user, $password) ;
or die ("Could Not Connect to DB");
?> </head>
<body>
*********** how will i use dbconnect in here **********
<?php
dbconnect($host,$user,$password);
?>
</body>
</html>
PHP/HTML reads a file from top to bottom I suspect so function definitions should be made before they are used. and I'm not sure about the orange semicolon
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 8th, 2005, 02:04 AM
#3
Thread Starter
Addicted Member
Re: function..
ok... i have solved most of my problems now.. umm how can i add items to a list on my form using the records from my database? <- got this working.. but the problem is.. the results are shown on a diffrent page.. i am wondering how can i expose all results on the same page.. i will post the code..
PHP Code:
<html>
<head>
</body>
<form>
<p>
<select name="mGrid" id="mGrid">
<option value="NONE">----- SELECT GRID -----</option>
<option value="NORTH">NORTH</option>
<option value="EAST">EAST</option>
<option value="SOUTH">SOUTH</option>
<option value="WEST">WEST</option>
</select>
<input type="button" name="Button" value="Button" onClick="show()"> ** how can i call show after clicking the button?? **
<br>
<br>
<select name="select" size="10">
<option>--------------- LIST OF BRANCHES ---------------</option>
</select>
</form>
</html>
<? function show(){
$host = 'localhost';
$user = 'root';
$password = 'admin';
$conn = mysql_connect($host, $user, $password)
or die ("Could Not Connect to DB");
$mooo = '$mGrid';
$sd = mysql_select_db("ifl-sales", $conn)
or die ("Could Not Select DB" . mysql_error());
$tableaccess = "select branch_name from branch where grid = '$mooo'";
$sb = mysql_query($tableaccess, $conn);
while ($row = mysql_fetch_array ($sb))
{
echo("<option>");
echo($row['branch_name']);
}
}
?>
everytime ic click the button nothing happens..
Last edited by titan7262; Sep 8th, 2005 at 04:45 AM.
-
Sep 8th, 2005, 08:12 AM
#4
<?="Moderator"?>
Re: function..
You are trying to call the PHP function from the client side, this wont work. All the PHP gets processed before it gets to the client. So they will never see your PHP code. Try using a form eg
PHP Code:
<form method="POST">
<input type="submit" name="submit1" value="Get Values"/>
<input type="hidden" name="action" value="getvalues" />
</form>
<?
if($_GET['action']=="getvalues")
{
show(); // <--- calling your function here;
}
?>
That will basicaly do what you want, but the page will be reloaded, which really cant be helped.
There is a way to get the data as you click the button, but that uses javascript as well and is a little complex, ill just find the link for you.
-
Sep 8th, 2005, 08:17 AM
#5
<?="Moderator"?>
-
Sep 8th, 2005, 07:02 PM
#6
Thread Starter
Addicted Member
Re: function..
<form method="POST">
<input type="submit" name="submit1" value="Get Values"/>
<input type="hidden" name="action" value="getvalues" />
</form>
<?
if($_GET['action']=="getvalues")
{
show(); // <--- calling your function here;
}
?>
for what is the <input type="hidden" name="action" value="getvalues" /> for? and how will my page be loaded i can't seem to find how it is reloaded..
so i need to learn javascript for me to be able to call php functions?
-
Sep 8th, 2005, 07:10 PM
#7
<?="Moderator"?>
Re: function..
Use use a form to submit the data to the php script. The <input type="hidden" name="action" value="getvalues" /> is so that the part of your script you want to run only happens after you press the button.
-
Sep 8th, 2005, 09:29 PM
#8
Thread Starter
Addicted Member
Re: function..
sorry.. but i can't get it to work..
<html>
<head>
</body>
<?php include("show.php")?>
<form method="post">
<p>
<select name="mGrid" id="mGrid">
<option value="NONE">----- SELECT GRID -----</option>
<option value="NORTH">NORTH</option>
<option value="EAST">EAST</option>
<option value="SOUTH">SOUTH</option>
<option value="WEST">WEST</option>
</select>
<input type="button" name="Button" value="Button">
<input type="hidden" name="action" value="Button">
<br>
<br>
<select name="select" size="10">
<option>--------------- LIST OF BRANCHES ---------------</option>
</select>
</form>
<?
if($_GET['action']=="button")
{
show();
}
?>
</html>
what's wrong with this.. it says error on line 23 which is if($_GET['action']=="button") <-- that's the line
-
Sep 8th, 2005, 09:36 PM
#9
<?="Moderator"?>
Re: function..
You need to change two things, one PHP is case sensative, and secondly you are not using a submit button on your form, you are using a normal button try
Code:
<html>
<head>
</body>
<?php include("show.php")?>
<form method="post">
<p>
<select name="mGrid" id="mGrid">
<option value="NONE">----- SELECT GRID -----</option>
<option value="NORTH">NORTH</option>
<option value="EAST">EAST</option>
<option value="SOUTH">SOUTH</option>
<option value="WEST">WEST</option>
</select>
<input type="submit" name="Button" value="Button">
<input type="hidden" name="action" value="button">
<br>
<br>
<select name="select" size="10">
<option>--------------- LIST OF BRANCHES ---------------</option>
</select>
</form>
<?
if($_GET['action']=="button")
{
show();
}
?>
</html>
That should work now
-
Sep 8th, 2005, 09:44 PM
#10
Thread Starter
Addicted Member
Re: function..
i am getting this error..
Notice: Undefined index: action in D:\PHPWEB\HTMLKIT\temp.php on line 24
line 24 is: if($_GET['action']=="button")
-
Sep 8th, 2005, 09:52 PM
#11
<?="Moderator"?>
Re: function..
ahhh im half asleep try using
PHP Code:
<?
if(isset($_POST['action']))
{
if($_POST['action']=="button")
{
show();
}
}
?>
-
Sep 8th, 2005, 10:02 PM
#12
Thread Starter
Addicted Member
Re: function..
ok that did the trick.. and a new error has occured.. sorry if i am bugging you this much.. 
this is the whole code..
PHP Code:
<html>
<head>
</body>
<?php include("show.php")?>
<form method="post">
<p>
<select name="mGrid" id="mGrid">
<option value="NONE">----- SELECT GRID -----</option>
<option value="NORTH">NORTH</option>
<option value="EAST">EAST</option>
<option value="SOUTH">SOUTH</option>
<option value="WEST">WEST</option>
</select>
<input type="submit" name="Button" value="Button">
<input type="hidden" name="action" value="button">
<br>
<br>
<select name="select" size="10">
<option>--------------- LIST OF BRANCHES ---------------</option>
</select>
</form>
<?
if(isset($_POST['action']))
{
if($_POST['action']=="button")
{
mshow()
}
}
?>
</html>
<?
function mshow(){
$host = 'localhost';
$user = 'root';
$password = 'admin';
$conn = mysql_connect($host, $user, $password)
or die ("Could Not Connect to DB");
$mooo = '$mGrid';
$sd = mysql_select_db("ifl-sales", $conn)
or die ("Could Not Select DB" . mysql_error());
$tableaccess = "select branch_name from branch where grid = '$mooo'";
$sb = mysql_query($tableaccess, $conn);
while ($row = mysql_fetch_array ($sb))
{
echo("<option>");
echo($row['branch_name']);
}
}
?>
nothing happens.. it just redraw the page but did not show the results..
Last edited by titan7262; Sep 8th, 2005 at 10:11 PM.
-
Sep 11th, 2005, 10:08 AM
#13
<?="Moderator"?>
Re: function..
You don't have superglobals turned on so change
PHP Code:
$mooo = '$mGrid';
to
PHP Code:
$mooo = $_POST['mGrid'];
It *should* work now.....hopefuly.
-
Sep 12th, 2005, 02:15 AM
#14
Thread Starter
Addicted Member
Re: function..
just found some samples on other websites.. Thanks to all that helped!!!
CHEERS!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|