2014-01-30 2 views
-2

나는 잠시 동안이 머리 위로 책상을 상대로 머리를 때려 눕혔다.HTML 텍스트 입력 필드에서 PHP 변수 저장하기

기본적으로 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> 
+0

그래서 당신이 양식을 제출 일어나면는? –

+0

어떤 라인에 변수를 추가 하시겠습니까? 또한 큰 코드에서 문제를 파악하는 것이 매우 어렵 기 때문에 관련 코드 만 게시하십시오. –

+0

끝에 을 추가해야합니다. $ service_url = 'http : //www.myapiservice/index.php/api/lookup? key = notreal & number ='; 정말 필요한 전부입니다. 나머지 코드는 의도 한대로 완벽하게 작동합니다. API url 끝에 텍스트를 추가 할 수 있어야합니다. – goddesspapa

답변

0

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> 
+0

Gowri 100 회 투표 할 수 있으면 좋겠어. 해결해 줘서 고마워! 나는 실제로 그 라인을 여섯 번 편집하려고했으나 다른 솔루션과 함께 서클에서 계속 뛰었습니다! – goddesspapa

+0

환영합니다 @ user3252650. 그럼 내 대답을 받아 들여라. – Gowri

관련 문제