passing php variable as function argument

php function arguments
php pass array by reference
php pass array to function
php pass function as parameter
php passing variables url
php pass by reference
php functions optional parameters
php named parameters

I have home.php and I am trying to pass two php variables in an onclick() function. But it does not work. I am new in php so any suggestion will be very helpful.

<form action="process/home.php" method="post">
        <input type="text" name="userfrom" value="<?php echo htmlspecialchars($username); ?>"/>
        <input type="submit" onclick=show('$user_id','$_SESSION['id']') value="Add Friend"/>
    </form>

In the show() function I wanted to store the values in the database.

function show($userfrom,$userto){

    $sql="INSERT INTO user (userfrom,userto) VALUES ('$userfrom','$userto')";
    mysql_query($sql);
    <!--it is not working-->
}

Your are new in php. So, remember this, you can't use PHP functions in JS code. onClick event needs js function. So firstly, you should pass your variables, with form then get it and after that you can use php functions.

You can use this code, but not recommended

<form action="" method="post">
    <input type="text" name="userfrom" value="<?= htmlspecialchars($username); ?>"/>
    <input type="submit" name="addFriend" />
</form>

<?php
    if (isset($_POST['addFriend'])) {
        show($user_id, $_SESSION['id']);
    }
?>

You should learn about jquery ajax. (Advice for you)

Function arguments - Manual, You can pass a variable by reference to a function so the function can modify the Function definitions alone are enough to correctly pass the argument by  By default, PHP variables are passed by value as the function arguments in PHP. When variables in PHP is passed by value, the scope of the variable defined at function level bound within the scope of function. Changing either of the variables doesn’t have any effect on either of the variables.

Try below code, You have some syntax errors,

<form action="process/home.php" method="post">
        <input type="text" name="userfrom" value="<?php echo htmlspecialchars($username); ?>"/>
        <input type="submit" onclick="show(<?php echo $userid ; ?>, <?php echo 
 $_SESSION['id']; ?>)" value="Add Friend"/>
    </form>

Passing by Reference - Manual, Your are new in php. So, remember this, you can't use PHP functions in JS code. onClick event needs js function. So firstly, you should pass  I have a PHP function that takes a variable number of arguments (using func_num_args() and func_get_args()), but the number of arguments I want to pass the function depends on the length of an arra

You need to echo your php variables into HTML elements.

onclick=show('<?=$user_id;?>','<?=$_SESSION['id'];?>')

shorthand for echo: <?=

and don't forget to add quotations when passing string values.

passing php variable as function argument, Example #2 Passing function parameters by reference. <?php function A function may define C++-style default values for scalar arguments as follows:. You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside.

Function arguments, I'm successfully passing 2 php variables to a javascript function. Code: PHP: $a; // this is name of employee $b; // this is address of employee  <?php /* This function internally swaps the contents between two simple variables using 'passing by reference'. Some programming languages have such a swap function built in, but PHP seems to lack such a function. So, one was created to fill the need. It only handles simple, single variables, not arrays, but it is still a very handy tool to have.

Passing php variable as Javascript argument, On this page we demonstrate and describe passing PHP variables containing simple To pass scalar data held in a PHP variable ( $val ) to a JavaScript variable, The PHP json_encode function can be used to resolve these problems as  I need to pass a function as a parameter to another function and then call the passed function from within the functionThis is probably easier for me to explain in code..I basically want to do

Passing PHP Variables to JavaScript, The ampersand before the parameter in the function definition is sufficient to pass the variable by reference. Many built-in PHP functions use pass  Accept function as parameter in PHP. function to create a function as a variable and pass it for a function that accepts a function(s) as an argument? 0.

Comments
  • but still not working the function.
  • It will not call php function, this is only called javascript function you need to use Ajax to call php function.