如何从外部页面获取并显示值到父页面的html元素

时间:2022-12-08 11:19:51

Here is my first.php and second.php

这是我的first.php和second.php

Where i am just submitting the form itself, but i am including the second.php file which has the function get() which wil return 'ok'.

我只是提交表单本身,但我包含了第二个.php文件,它具有函数get(),它将返回'ok'。

So I am calling the get() function after the form is posted.

所以我在发布表单后调用了get()函数。

Now how can I get the values and display it in the text fetchedvalue ?

现在我如何获取值并将其显示在文本fetchedvalue中?

first.php

<?php
include 'second.php';
?>
<form method="post" action="">
<input type="text" name="fetchedvalue">
<input type="submit" name="login" value="Submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
get();
}
?>

second.php

<?php
function get()
{
  echo 'ok';
}

Note :

I have many data like that, How can I do it in a single form submit and fill all such textbox ?

我有很多这样的数据,我怎样才能以单一形式提交并填写所有这样的文本框?

4 个解决方案

#1


try using this method:

尝试使用此方法:

<?php
  include 'second.php';
  $fvalue = '';
  if(isset($_POST['fetchedvalue']))
    $fvalue = $_POST['fetchedvalue'];
?>
<form method="post" action="">
  <input type="text" name="fetchedvalue" value="<?=$fvalue;?>">
  <input type="submit" name="login" value="Submit">
</form>
<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST')
  {
    get();
  }
?>

notice that $fvalue is the variable responsible for creating value in the "fetchedvalue" input tag.. if the post is still not done, then the value would be "".

请注意,$ fvalue是负责在“fetchedvalue”输入标记中创建值的变量。如果帖子仍未完成,则值为“”。

#2


$_POST['fetchedvalue'] will contain it.

$ _POST ['fetchedvalue']将包含它。

#3


<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login']))
{
    echo "<pre>";
    print_r($_POST); // Here you will get all posted values & then you can call get method
    echo "</pre>";
    get();   
}
?>

#4


You can access the variables by using $_POST like an array, so:

您可以使用$ _POST像数组一样访问变量,因此:

$fetchedValue = $_POST['fetchedvalue'];

Though, a safer approach would be the following:

但是,更安全的方法如下:

<?php
include 'second.php';

$fetchedValue = filter_input(INPUT_POST, 'fetchedvalue', FILTER_SANITIZE_STRING);

?>

<form method="post" action="">
    <input type="text" name="fetchedvalue" value="<?php echo $fetchedValue; ?>">
    <input type="submit" name="login" value="Submit">
</form>

filter_input() will return null if no POST variable with the given name is defined, and if it is, it will apply some sanitation or validation on it. Have a look at the manual for more detailed information:

如果没有定义具有给定名称的POST变量,filter_input()将返回null,如果是,则将对其应用一些卫生或验证。有关更多详细信息,请查看手册:

http://php.net/filter_input

#1


try using this method:

尝试使用此方法:

<?php
  include 'second.php';
  $fvalue = '';
  if(isset($_POST['fetchedvalue']))
    $fvalue = $_POST['fetchedvalue'];
?>
<form method="post" action="">
  <input type="text" name="fetchedvalue" value="<?=$fvalue;?>">
  <input type="submit" name="login" value="Submit">
</form>
<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST')
  {
    get();
  }
?>

notice that $fvalue is the variable responsible for creating value in the "fetchedvalue" input tag.. if the post is still not done, then the value would be "".

请注意,$ fvalue是负责在“fetchedvalue”输入标记中创建值的变量。如果帖子仍未完成,则值为“”。

#2


$_POST['fetchedvalue'] will contain it.

$ _POST ['fetchedvalue']将包含它。

#3


<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login']))
{
    echo "<pre>";
    print_r($_POST); // Here you will get all posted values & then you can call get method
    echo "</pre>";
    get();   
}
?>

#4


You can access the variables by using $_POST like an array, so:

您可以使用$ _POST像数组一样访问变量,因此:

$fetchedValue = $_POST['fetchedvalue'];

Though, a safer approach would be the following:

但是,更安全的方法如下:

<?php
include 'second.php';

$fetchedValue = filter_input(INPUT_POST, 'fetchedvalue', FILTER_SANITIZE_STRING);

?>

<form method="post" action="">
    <input type="text" name="fetchedvalue" value="<?php echo $fetchedValue; ?>">
    <input type="submit" name="login" value="Submit">
</form>

filter_input() will return null if no POST variable with the given name is defined, and if it is, it will apply some sanitation or validation on it. Have a look at the manual for more detailed information:

如果没有定义具有给定名称的POST变量,filter_input()将返回null,如果是,则将对其应用一些卫生或验证。有关更多详细信息,请查看手册:

http://php.net/filter_input