Jump to content

Passing Values to Functions


craigafer

Recommended Posts

hi guys :D

im back for more php questions

hope you dont get annoyed

 

<?php
function getvalue($num)
{
echo "<input type='text' name='txt1' value='$num'>";
}
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="txt1" id="txt1" value="0">
<input type="button" name="btn1" id="btn1"  value="1" onclick="getvalue(1);">
<input type="button" name="btn2" id="btn2"  value="2" onclick="getvalue(2);">
</form>
</body>
</html>

 

here is my code

my question is

  • how can i pass the values at the php function?
  • how can i display the passed value to the textbox txt1? is this code correct? echo "<input type='text' name='txt1' value='$num'>";

 

Link to comment
Share on other sites

you place a variable in the the piece between ()

 

your function is:

<?php
function getvalue($num)
{
echo "<input type='text' name='txt1' value='$num'>";
}
?>

 

So you would do:

$string = 'mooOOoO';

getvalue($string);

 

There is also a way with return() (within the function) which you might want to look in.

Link to comment
Share on other sites

<input type="button" name="btn1" id="btn1"  value="1" onclick="getvalue(1);">

<input type="button" name="btn2" id="btn2"  value="2" onclick="getvalue(2);">[/code]

 

That is not going to call your PHP function. The web browser is going to assume that getvalue() call is a javascript function and throw an error saying getvalue() is not defined.

 

To call your PHP function from there you need to enclose the function call inside normal <?php ?> tags. That said, if you used the correct syntax there it would not make sense to be inserting an <input> tag inside an onclick attribute.

Link to comment
Share on other sites

<input type="button" name="btn1" id="btn1"  value="1" onclick="getvalue(1);">

<input type="button" name="btn2" id="btn2"  value="2" onclick="getvalue(2);">[/code]

 

That is not going to call your PHP function. The web browser is going to assume that getvalue() call is a javascript function and throw an error saying getvalue() is not defined.

 

To call your PHP function from there you need to enclose the function call inside normal <?php ?> tags. That said, if you used the correct syntax there it would not make sense to be inserting an <input> tag inside an onclick attribute.

 

is this correct now?

i've tried what you've said

here is the code: its a simple display of 1 and 2

<?php
session_start();
$display = 0;
global $display;
function getvalue($num)
{	
if($num==1)	
{	
$display.="1";
}
else
{
$display.="2";
};
}
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="calcu.php" method="POST">
<input type="text" name="display" id="display" readonly="readonly" value="<?php echo $display?>">
<input type="submit" name="btn" value="1" onclick="<?php getvalue(1)?>">
<input type="submit" name="btn" value="2" onclick="<?php getvalue(2)?>">
</form>
</body>
</html>

 

but it doesnt display 1 or 2

 

Link to comment
Share on other sites

<?php
session_start();
$display = 0;

function getvalue($num)
{	
if($num==1)	
{	
$display ="1";
}
else
{
$display ="2";
};
return $display;
}
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="calcu.php" method="POST">
<input type="text" name="display" id="display" readonly="readonly" value="<?php echo $display; ?>">
<input type="submit" name="btn" value="1" onclick="<?php echo getvalue(1); ?>">
<input type="submit" name="btn" value="2" onclick="<?php echo getvalue(2)?>">
</form>
</body>
</html>

Link to comment
Share on other sites

Hi there,

<?php
session_start();
$display = 0;
global $display;
function getvalue($num){
if($num==1){
   return $display.="1";
}
else{
return $display.="2";
};
}
?>

 

You need to return from a function if the data is held in a var & not echoed

 

EDIT: Beaten to it!

 

Cheers,

Rw

Link to comment
Share on other sites

tried what you guys said

but it doesnt work

:-[

<?php
session_start();
$display = 0;
global $display;
function getvalue($num)
{	
if($num==1)	
{	
$display.="1";
}
else
{
$display.="2";
};
return($display);
}
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="calcu.php" method="POST">
<input type="text" name="display" id="display" readonly="readonly" value="<?php echo $display?>">
<input type="submit" name="btn" value="1" onclick="<?php getvalue(1);?>">
<input type="submit" name="btn" value="2" onclick="<?php getvalue(2);?>">
</form>
</body>
</html>

Link to comment
Share on other sites

What you're trying won't work because JavaScript and PHP aren't the same thing.  PHP is finished processing by the time JavaScript enters the scene.  Therefore, you can't plug a PHP function into a JavaScript event (at least, not without doing things that are clearly beyond your present ability).

 

To further clarify, your PHP function will fire twice.  In both cases, if you're lucky, the proper return value will then be assigned to the JavaScript onclick event (I actually think it will be a null result, since you don't assign your return value to anything in PHP, but it doesn't really matter).  onclick=1 and onclick=2 doesn't mean anything.  It's gibberish, and won't magically set the values you want on the page.

 

And this isn't even touching on you trying to use 'global' (in short, don't do it.  it's a crutch, and a bad one at that.  learn to write functions properly) or the syntax errors.

 

What you should do is get a couple of good beginner PHP and JavaScript books, because you have some misunderstandings at the fundamental level which are too broad and deep to properly address on a forum.

Link to comment
Share on other sites

theres a couple of php books here what should i get?

 

thats the only problem i have passing values to functions  :-[

 

and displaying it

 

No offense, but since you clearly don't grasp the difference between PHP and JavaScript, let alone how to use them together, I'd say you have more problems than just writing functions.

 

As for books, the Visual Quickstart Guide for PHP is pretty good.  In general, books from either O'Reilly Publishing or APress Publishing are good.

Link to comment
Share on other sites

alright i will look for that book

but can you please have a solution for my problem?

 

It's unclear what the problem is and so not possible to provide a solution. It looks as though you're trying to change the label of the button when it is clicked, is that correct? From the code you're putting numbers or a string into the onclick attribute of a button - this makes no sense and there is no logical reason to do that.

Link to comment
Share on other sites

<?php
session_start();
$display = 0;
global $display;
function getvalue($num)
{	
if($num==1)	
{	
$display.="1";
}
else
{
$display.="2";
};
return($display);
}
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="calcu.php" method="POST">
<input type="text" name="display" id="display" readonly="readonly" value="<?php echo $display?>">
<input type="submit" name="btn" value="1">
<input type="submit" name="btn" value="2">
</form>
</body>
</html>

edited it sorry for being so noob -__-

got no reference to study

Link to comment
Share on other sites

Solution:

 

<?php
   if(isset($_POST['submit']))
   {
      $buttonVal = $_POST['num'];
   }
?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
   </head>

   <body>
      <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
         1 <input name="num" type="radio" value="1" />
         2 <input name="num" type="radio" value="2" />
         <br /><br />
         Number entered: <input name="display" type="text" value="<?php if(isset($buttonVal)){ echo $buttonVal; } ?>" />
         <input name="submit" type="submit" value="Submit" />
      </form>
   </body>

</html>

 

Notice that there's:

 

1. No function.  For something this simple, no function is necessary.

2. No JavaScript, which was one of your main problems from before.

Link to comment
Share on other sites

sorry for the super late reply i became busy so i kinda tryin to edit what NightSlyr posted but again i failed

<?php
   if(isset($_POST['submit']))
   {
      $buttonVal = $_POST['btn1'];
   }
?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
   </head>

   <body>
      <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
         Display: <input name="display" type="text" value="<?php if(isset($buttonVal)){ echo $buttonVal; } ?>" />
         <input type="submit" value="1" name="btn1" id="btn1" />
      </form>
   </body>

</html>

how can i display the value of the button everytime i clicked it?

Link to comment
Share on other sites

sorry for the super late reply i became busy so i kinda tryin to edit what NightSlyr posted but again i failed

<?php
   if(isset($_POST['submit']))
   {
      $buttonVal = $_POST['btn1'];
   }
?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
   </head>

   <body>
      <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
         Display: <input name="display" type="text" value="<?php if(isset($buttonVal)){ echo $buttonVal; } ?>" />
         <input type="submit" value="1" name="btn1" id="btn1" />
      </form>
   </body>

</html>

how can i display the value of the button everytime i clicked it?

 

:facepalm:

 

You don't have a button named submit in your form now.  That means your PHP code at the top will never work.

 

The superglobal $_POST array works by using the names of your HTML form inputs.  $_POST['submit'] maps to <input type="submit" name="submit" value="submit" />, just as $_POST['username'] would map to something like <input type="text" name="username" />.  Take note of the parts that are bold.

 

Have you bought that book yet?  You'll learn a lot more with a good book than reading free tutorials of questionable quality and throwing a bunch of code at the wall to see what actually sticks.

Link to comment
Share on other sites

sorry for the super late reply i became busy so i kinda tryin to edit what NightSlyr posted but again i failed

<?php
   if(isset($_POST['submit']))
   {
      $buttonVal = $_POST['btn1'];
   }
?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
   </head>

   <body>
      <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
         Display: <input name="display" type="text" value="<?php if(isset($buttonVal)){ echo $buttonVal; } ?>" />
         <input type="submit" value="1" name="btn1" id="btn1" />
      </form>
   </body>

</html>

how can i display the value of the button everytime i clicked it?

 

:facepalm:

 

You don't have a button named submit in your form now.  That means your PHP code at the top will never work.

 

The superglobal $_POST array works by using the names of your HTML form inputs.  $_POST['submit'] maps to <input type="submit" name="submit" value="submit" />, just as $_POST['username'] would map to something like <input type="text" name="username" />.  Take note of the parts that are bold.

 

Have you bought that book yet?  You'll learn a lot more with a good book than reading free tutorials of questionable quality and throwing a bunch of code at the wall to see what actually sticks.

 

i cant find the book you were saying here  :shrug:

 

i got your point so if i renamed if(isset($_POST['submit'])) to if(isset($_POST['btn1'])) will it display the value of btn1?

Link to comment
Share on other sites

i cant find the book you were saying here  :shrug:

 

Book: http://www.amazon.com/gp/product/0321245652/ref=s9_simh_gw_p14_d5_i1?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0HH9YVTWDYSW8PDR0NB9&pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846

 

That's about as good a primer on beginning PHP you can find.

 

i got your point so if i renamed if(isset($_POST['submit'])) to if(isset($_POST['btn1'])) will it display the value of btn1?

 

Yes, that's right.

Link to comment
Share on other sites

last 1 and it will be marked as solved!! :D

i will buy the book as soon as possible

<?php
session_start();
$buttonVal = 0;
$compute = 0;
$newnum = 0;
$display =0;
$text = 0;
global $buttonVal, $compute, $newnum, $display, $text;
   if(isset($_POST["btn1"]))
   {
	$display=$_POST["btn1"];
	setNum($display);
   }


function setNum($text)
{
if($newnum==0)
	{
		$buttonVal = $text;
		$newnum=1;
	}
else
	{
		$buttonVal = $buttonVal . $text;
	}
   }
   
   
?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
   </head>

   <body>
      <form action="omg.php" method="post">
         Display: <input name="display" type="text" readonly="readonly" value="<?php echo $buttonVal; ?>" /><br>
         <input type="submit" value="1" name="btn1" id="btn1" />
	 <input type="submit" value="2" name="btn1" id="btn1" />
	<input type="submit" value="3" name="btn1" id="btn1" />
	 <input type="submit" value="4" name="btn1" id="btn1" />
	 <input type="submit" value="5" name="btn1" id="btn1" /><br>
	 <input type="submit" value="6" name="btn1" id="btn1" />
	 <input type="submit" value="7" name="btn1" id="btn1" />
	 <input type="submit" value="8" name="btn1" id="btn1" />
	 <input type="submit" value="9" name="btn1" id="btn1" />
	 <input type="submit" value="0" name="btn1" id="btn1" /><br>
	 <input type="submit" value="+" name="btn2" id="btn2" />
	 <input type="submit" value="-" name="btn2" id="btn2" />
	 <input type="submit" value="*" name="btn2" id="btn2" />
	 <input type="submit" value="/" name="btn2" id="btn2" />
	 <input type="submit" value="=" name="btn2" id="btn2" />

      </form>
   </body>

</html>

 

im done with the displayin, but whats the prob here nao? it doesnt display the $buttonVal but when you echo it, it echoes the right value

Link to comment
Share on other sites

last 1 and it will be marked as solved!! :D

i will buy the book as soon as possible

<?php
session_start();
$buttonVal = 0;
$compute = 0;
$newnum = 0;
$display =0;
$text = 0;
global $buttonVal, $compute, $newnum, $display, $text;
   if(isset($_POST["btn1"]))
   {
	$display=$_POST["btn1"];
	setNum($display);
   }


function setNum($text)
{
if($newnum==0)
	{
		$buttonVal = $text;
		$newnum=1;
	}
else
	{
		$buttonVal = $buttonVal . $text;
	}
   }
   
   
?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
   </head>

   <body>
      <form action="omg.php" method="post">
         Display: <input name="display" type="text" readonly="readonly" value="<?php echo $buttonVal; ?>" /><br>
         <input type="submit" value="1" name="btn1" id="btn1" />
	 <input type="submit" value="2" name="btn1" id="btn1" />
	<input type="submit" value="3" name="btn1" id="btn1" />
	 <input type="submit" value="4" name="btn1" id="btn1" />
	 <input type="submit" value="5" name="btn1" id="btn1" /><br>
	 <input type="submit" value="6" name="btn1" id="btn1" />
	 <input type="submit" value="7" name="btn1" id="btn1" />
	 <input type="submit" value="8" name="btn1" id="btn1" />
	 <input type="submit" value="9" name="btn1" id="btn1" />
	 <input type="submit" value="0" name="btn1" id="btn1" /><br>
	 <input type="submit" value="+" name="btn2" id="btn2" />
	 <input type="submit" value="-" name="btn2" id="btn2" />
	 <input type="submit" value="*" name="btn2" id="btn2" />
	 <input type="submit" value="/" name="btn2" id="btn2" />
	 <input type="submit" value="=" name="btn2" id="btn2" />

      </form>
   </body>

</html>

 

im done with the displayin, but whats the prob here nao? it doesnt display the $buttonVal but when you echo it, it echoes the right value

 

1. Never, ever, ever use 'global' to pass data into a function.  It's bad programming, pure and simple.  Use the function's argument list.  That's why it's there.

 

2. You don't use most of the variables you declare.

 

3. HTML elements cannot share names or ids.  Think about it - a name or an id is supposed to be a unique identifier, right?  Unique means one and only one.

 

4. Have you tried executing your code more than once?  From what I can see, the display will only ever show 1 because you never assign the old values to anything.  $text, in particular, is never initialized or assigned to.

 

Like Thorpe said, re-read our earlier replies.  I gave you a solution that works and can be easily modified to suit your needs.  We're not going to spoon-feed you the answer, especially since this appears to be a homework problem.

 

Instead of panicking, take a breath and truly look at the code I gave you.  You have everything except the final solution.  Try to see how it all connects.

Link to comment
Share on other sites

i got your point so if i renamed if(isset($_POST['submit'])) to if(isset($_POST['btn1'])) will it display the value of btn1?

 

If you're just checking if a form has been posted use

 

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
   // it's submitted
}

 

This is better than if(isset($_POST['submit'])) or if(isset($_POST['btn1'])) because it's not reliant on a single form field.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.