Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been bashing my head against a desk for awhile over this one and my brain is mush from reading tons of stuff and not having anything work.

Basically, I need to take the input from an html input field, assign it to a php variable, then append that variable to the end of a url. I'm not having much luck at all so far!

<!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>
share|improve this question
 
So what happens once you submit the form? –  I Can Has Cheezburger Jan 30 at 10:40
 
In which line do you want to add the variable. Also please post only relevant code as it's very difficult to figure out your problem in such a large code. –  Sankalp Mishra Jan 30 at 10:40
 
I need to add <text_field_input> to the end of: $service_url = 'myapiservice/index.php/api/lookup?key=notreal&number=';; That's all I really need. The rest of the code works perfectly as intended, I just need to be able to append the text to the end of the API url. –  user3252650 Jan 30 at 10:43
 
PHP is a server side language. That means that, unlike you may expect, things are only evaluated once and then sent to your browser. Once the info is sent to your browser (that is, once you see it) PHP will forget about it, and that's all. Have you tried submitting your form?. It is not until your code is evaluated again (this time with user input) that you may able to see the results. –  The Marlboro Man Jan 30 at 10:51
 
From my understanding, the reasoning behind using POST is it allows it to be updating after page load (in the case of reading input from a text field). I have submitted it close to 100 times so far with various tweaks, and have no luck getting it to update correctly. I really tried to avoid asking the question on here but got frustrated after 8 hours of reading and trying code snippets with no break through. –  user3252650 Jan 30 at 11:00
show 1 more comment

1 Answer

up vote 0 down vote accepted

You are using modalbox for input type=submit. So it is not send your data after click on submit.

I have modified submit button and open modal box upon submit button using javascript.

Here is your code:

<!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>
share|improve this answer
 
Gowri I wish I could vote you up 100 times, thank you for resolving this! I actually meant to edit that line half a dozen times but kept running myself in circles with other solutions! –  user3252650 Jan 30 at 11:15
 
Your welcome @user3252650. Then accept my answer. –  Gowri Jan 30 at 11:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.