1

I have a file called function.php that has a function like this:

public function issue_challenge () 
{
    $conn= fns_connect(13);
    mysql_select_db("et_games",$conn);
    $sql = "insert into challengers (challenger,defender,stake,matching_game_id) values (".$this->talentnum_self.",".$this->talentnum_other.",".$this->stake.",'".$this->matching_str()."')";
    $result = mysql_query($sql);
    echo $sql; mysql_error();

    if($result){
        echo "ok.\n";return true;                
    }else{
        return false;                    
    }
}

and another file called test.php that has a form in it.

How do I pass the values from that form into that function?

I am looking for some generic answer on how to pass a string/variable to a function.

Thanks

1
  • functionName('I am a string'); to pass a string, functionName($iAmAVariable); to pass a variable Commented May 9, 2011 at 17:14

3 Answers 3

1

Call your function this way:

 if(isset($_GET['form_name'])) {
     $val = $_GET['form_name'];
     public function issue_challenge($val);
}

Also: Change the declaration to:

public function issue_challenge($formValue) {

...function contents...

}
Sign up to request clarification or add additional context in comments.

Comments

1

Generic answer that increments a value:

function inc($x) {

 echo "x = " . $x;
 $x++;
 return $x; 

}

function main() {

$x = '1';
$y = $this->inc($x);
echo $y; 

}

Comments

1

Make sure the form submission calls the function, then grab the value from the POST array. This is easier if using a php framework, many will offer you methods to clean the input. In this case since you are possibly using the input for a db query you will need to make sure it is properly escaped to avoid sql injection.

PHP manaul for mysql_real_escape_string

<form action="issue_challenge" method="POST">
<input type="text" name="testInput" id="testInput"/>
<input type="submit" name="submit" value="submit"/>
</form>

public function issue_challenge () {
    $conn= fns_connect(13);
    $inputText = $_POST['testInput']
mysql_select_db("et_games",$conn);
    $sql = "insert into challengers (challenger,defender,stake,matching_game_id) values (".$this->talentnum_self.",".$this->talentnum_other.",".$this->stake.",'".$this->matching_str()."')";
    $result = mysql_query($sql);
    echo $sql; mysql_error();
        if($result){
            echo "ok.\n";return true;                
            }else{
                return false;                    
            }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.