Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?


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

1 Answer

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 />';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...