SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question using array in a function

    hi everyone.

    i want to use an array ( already created outside the function ) in a function .

    here is the code

    page1.php
    HTML Code:
    <form action="page2.php" method="post">
    Marks1 = <input type="text" name="marks1" />
    Marks2 = <input type="text" name="marks2" />
    <input type="submit">
    page2.php
    PHP Code:
    extract($_POST);

    $answer=array("$marks1","$marks2");

    function 
    numcheck($val,$answer)
    {
        if(
    is_numeric($val))
        {
            return 
    $val;
             }
        else
        {
        
    $newanswer=serialize($answer);
        
    $encode=urlencode($newanswer);
        
    header('location:level1test?num=numerror&encode=$encode');
        }
    }

    $result=array_filter($answer,"numcheck"); 
    i am getting this error :- Missing argument 2 for numcheck()



    i tried to use array inside the function also like this


    PHP Code:
    function numcheck($val,$answer)
    {
        if(
    is_numeric($val))
        {
            return 
    $val;
             }
        else
        {

    $answer=array("$marks1","$marks2");

        
    $newanswer=serialize($answer);
        
    $encode=urlencode($newanswer);
        
    header('location:level1test?num=numerror&encode=$encode');
        }

    But now the error is undefined varible marks1 and marks2





    How to get the $answer array inside the function ??

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi, welcome to the forums!

    The problem you're having is because array_filter only passes one argument (an array element) to the function that you use with it.

    Will there always be just two values returned from the form (i.e. marks1 and marks2), or that's just an example and the actual form will have more?
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."

  3. #3
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi , thanks for reply ,,
    There are 30 values returned from the form ..

    i just want to use the $answer array inside the function numcheck

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Why not just loop over the array with foreach? I'm not sure why you're using extract to get the values from $_POST and then putting them back into an array?

    If you named your input elements as an array, it would be easy to retrieve just the answers in your script:
    HTML Code:
    <input type="text" name="answer[mark1]">
    <input type="text" name="answer[mark2]">
    <input type="text" name="answer[mark3]">
    PHP Code:
    $answers $_POST['answers'];

    $results = array();
    foreach (
    $answers as $question => $value)
    {
        
    // process results

    Also, I'm confused about what you're trying to do here:
    PHP Code:
    $newanswer serialize($answer);
    $encode urlencode($newanswer);
    header('location:level1test?num=numerror&encode=$encode'); 
    Are you wanting to send back the names of the invalid inputs?
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."

  5. #5
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i am sending back the names..for invalid input.. so serializing it before sending it through url.. and your answer regarding declare array in name itself.. is also interesting


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •