Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

get value from method html text field in php not working

I am trying to get a value of a text field after clicking a button from html in php. I tried it with the get and post method, but nothing worked. My code is:

<!DOCTYPE html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>

<div class="btn-group" role="group" aria-label="...">
    <form method="post">    
        <input type="submit" name="test" id="test" class="btn btn-info" value="RUN" /><br/>
    </form> 
</div>

<div class="formoutline">
    <form action="" method="get">
        <input name="subject2"type="text" id="subject2" class="form-control" />
    </form>
</div>  

<?php

function testfun()
{
    echo $_GET["subject2"];
}

if(array_key_exists('test',$_POST)){
   testfun();
}
?>

Can anyone help me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

You have two separate forms, but assuming you were trying to test two different methods, this should show you the way.

<!DOCTYPE html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>

<div class="btn-group" role="group" aria-label="...">
    <form method="post">    
        <input type="text" name="test" id="test" class="btn btn-info" /><br/>
        <input type="submit" name="post_submit" value="Submit via Post" />
    </form> 
</div>

<div class="formoutline">
    <form method="get">
        <input name="test" type="text" id="subject2" class="form-control" />
        <input type="submit" name="get_submit" value="Submit via Get" />
    </form>
</div>  

<?php

echo 'Get Form: ' . (filter_input(INPUT_GET, 'test') ?: 'Not Submitted') . '<br />';
echo 'Post Form: ' . (filter_input(INPUT_POST, 'test') ?: 'Not Submitted') . '<br />';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...