简体   繁体   English

通过php将参数从表单传递到另一个php文件

[英]Pass parameter through php vaiable from form to another php file

I have next html form: 我有下一个HTML表单:

<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. 我想将值从文本框“ uri1”传递给php变量。

Thats my frames1.php: 那就是我的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? 如何从文本框中传递值以读取php文件?

Thanks! 谢谢!

You are using method as post in form co change this to 您正在使用方法作为表单中的post,将其更改为

<?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 . 您通过POST发送表单,但在您的frames1.php文件中,尝试使用$_GET获取内容。 So what you have to do is just change GET to POST , so it looks like 所以,您要做的就是将GET更改为POST ,这样看起来

<?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. 还有一个更大的问题是,您的输入没有名称,只能由名称而不是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. HTML和PHP代码都有一个错误,您必须在输入字段中输入名称。 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. 表单提交到服务器后,它将使用HTML输入字段名称识别获取值,如果您发布的表单为HTML,则必须使用相同的方法来获取值。

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: 来到您的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: HTML文件中的差异为:

 <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. 在PHP中:您使用POST方法,但使用_GET []获取值,两者都是不兼容的方法。 So you will not get the values which are submitted by the form 因此,您将不会获得表单提交的值

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

You have set form method="post" . 您已设置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. 1)要么更改您的表单方法method="post"以使您现有的代码正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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