简体   繁体   中英

Pass parameter through php vaiable from form to another php file

I have next html form:

<form class="form-horizontal" action="frames1.php" method="post">
                    <div class="form-group">
                        <label for="uri1" class="control-label">URL:</label>
                        <div class="controls">
                            <input type="text" id="uri1" placeholder="www.ejemplo.com">
                        </div>
                    </div>                  
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <input type="submit" class="btn btn-primary" id="submit" value="Visualizar" />
                    </div>
</form>

I want to pass the value from textbox "uri1" to a php variable.

Thats my frames1.php:

<?php
 $uriValue = $_GET["uri1"];
?>

<script>
var uri = '<?php echo $uriValue;?>';
alert(uri);
</script>

But the system shows me "", not the value I put on the textbox in the form just before.

How can pass the value from the textbox to read on the php file?

Thanks!

You are using method as post in form co change this to

<?php
    $uriValue = $_POST["uri1"];
 ?>

<script>
    var uri = '<?php echo $uriValue;?>';
    alert(uri);
</script>

and also add name attribute to your field. name="uri1"

that is,

<form class="form-horizontal" action="frames1.php" method="post">
     <div class="form-group">
          <label for="uri1" class="control-label">URL:</label>
             <div class="controls">
                  <input type="text" id="uri1" name="uri1" placeholder="www.ejemplo.com">
              </div>
      </div>                  
      <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <input type="submit" class="btn btn-primary" id="submit" value="Visualizar" />
       </div>
</form>

Here is the working link.

You send a form by POST but in your frames1.php file, you try to get the content with $_GET . So what you have to do is just change GET to POST , so it looks like

<?php
   $uriValue = $_POST["uri1"];
?>

Also a bigger problem is, that your input doesn't have a name, the parameter can only be referenced by the name, not by the ID. So your input should look like that:

<input type="text" id="uri1" name="uri1" placeholder="www.ejemplo.com">

There are one mistake in HTML as well as PHP code, You have to give name to the input field's. When form is submitted to the server then It will recognize fetch values using HTML Input Field names, and If your posted form in HTML, then you have to use same method to get the values.

Try to replace bellow code with your code:

<form class="form-horizontal" action="frames1.php" method="post">
                    <div class="form-group">
                        <label for="uri1" class="control-label">URL:</label>
                        <div class="controls">
                            <input type="text" name="uri1" id="uri1" placeholder="www.ejemplo.com">
                        </div>
                    </div>                  
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <input type="submit" class="btn btn-primary" id="submit" value="Visualizar" />
                    </div>
</form>

Coming to your frames1.php:

<?php
 $uriValue = $_POST["uri1"];
?>

<script>
var uri = '<?php echo $uriValue;?>';
alert(uri);
</script>

Let me know still not resolved.

Diff in HTML file is:

 <input type="text" name="uri1" id="uri1" placeholder="www.ejemplo.com">

you didn't mention the name, then server will not recognize the element.

In PHP: Your using POST method but your getting the values using _GET[], both are incompatible methods. So you will not get the values which are submitted by the form

<?php
 $uriValue = $_POST["uri1"];
?>

You have set form method="post" .

Therefore, after your form gets submitted, the value should be:

$uriValue = $_POST["uri1"];

Not

$uriValue = $_GET["uri1"];

Solutions:

1) Either change your form method method="post" to get your existing code working.

2) Change $uriValue = $_GET["uri1"]; to $uriValue = $_POST["uri1"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM