从HTML文本输入字段存储PHP变量

时间:2014-01-30 10:34:21

标签: php html twitter-bootstrap variables input

我已经在这张桌子上砸了一会儿,我的大脑很难读取大量的东西,没有任何工作。

基本上,我需要从html输入字段获取输入,将其分配给php变量,然后将该变量附加到url的末尾。到目前为止,我一点都没有运气!

<!DOCTYPE html>
<html>
  <head>
    <title>MyPage</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/black-tie/jquery-ui.min.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
  </head>
  <!-- Sorry this is so ugly, I was trying to get it done fast, I can make another pass and smack it with a pretty stick later -->
  <body>
  <div class="jumbotron">
    <div class="container">
      <h1>My Super Page</h1>
      <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
        <p class="lead">Please Enter Some Text: </p>
        <input type="text" name="input">
        <input type="submit" data-toggle="modal" data-target="#myModal">
      </form>
      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">Results</h4>
            </div>
            <div class="modal-body">
              Your Input Parsed:
              <?php
              $myInput = $_POST["input"];
              echo $myInput; 
              $service_url = 'http://www.myapiservice/index.php/api/lookup?key=notreal&number='. $myInput;
              $curl = curl_init($service_url);
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
              $curl_response = curl_exec($curl);
              if ($curl_response === false) {
                  $info = curl_getinfo($curl);
                  curl_close($curl);
                  die('error occured during curl exec. Additional info: ' . var_export($info));
              }
              curl_close($curl);
              echo $name;
              $decoded = json_decode($curl_response, true);
              if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
                  die('error occured: ' . $decoded->response->errormessage);
              }
              $myVar = $decoded["Response"]["dataset"];
              if($myVar){
                echo $myVar;
              }
              else {
                echo 'Discarded Data';
              }

              ?>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
      </div><!-- /.modal -->
    </div>
  </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

您正在使用modalbox输入type=submit。因此,点击提交后不会发送您的数据。

我已使用javascript.

修改了提交按钮并在提交按钮时打开了模式框

这是你的代码:

<!DOCTYPE html>
<html>
  <head>
    <title>MyPage</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/black-tie/jquery-ui.min.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <script src="http://jquery-jsonp.googlecode.com/files/jquery.jsonp-1.0.4.min.js"></script>
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
  </head>


  <!-- Sorry this is so ugly, I was trying to get it done fast, I can make another pass and smack it with a pretty stick later -->
  <body>
  <div class="jumbotron">
    <div class="container">
      <h1>My Super Page</h1>
      <form method="get" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
        <p class="lead">Please Enter Some Text: </p>
        <input type="text" name="input">
        <input type="submit">
      </form>

       <?php if(isset($_REQUEST['input'])){?>
  <script>
    $(function() {
          $( "#myModal" ).modal('show');   
    });
  </script>
  <?php }?>

      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">Results</h4>
            </div>
            <div class="modal-body">
              Your Input Parsed:
              <?php
              $myInput = $_REQUEST['input'];
              echo $myInput; 
              $service_url = 'http://www.myapiservice/index.php/api/lookup?key=notreal&number='. $myInput;
              $curl = curl_init($service_url);
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
              $curl_response = curl_exec($curl);
              if ($curl_response === false) {
                  $info = curl_getinfo($curl);
                  curl_close($curl);
                  die('error occured during curl exec. Additional info: ' . var_export($info));
              }
              curl_close($curl);
              echo $name;
              $decoded = json_decode($curl_response, true);
              if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
                  die('error occured: ' . $decoded->response->errormessage);
              }
              $myVar = $decoded["Response"]["dataset"];
              if($myVar){
                echo $myVar;
              }
              else {
                echo 'Discarded Data';
              }

              ?>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
      </div><!-- /.modal -->
    </div>
  </div>
  </body>
</html>