PHP:如何使用PHP中的GET将值存储到数组中

时间:2022-09-25 22:13:47

I want to refresh on the same page.I have script that allows the user to add in values in a text field then onclick displays the list at the bottom the same page.I want add in values on the same page. The array is not updating as I click and php.net is been blocked over my side.

我想在同一页面刷新。我有脚本允许用户在文本字段中添加值,然后onclick显示同一页面底部的列表。我想在同一页面上添加值。当我点击并且php.net被阻挡在我身边时,数组没有更新。

<?php 
session_start();
$_SESSION['mylist'] ;
$mylist = array();
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form id="form1" name="form1" method="GET" action="">
  <tr>
    <td width="30%">
      <label for="ref_num">Custom Number</label>
      <input type="text" name="ref_num" id="ref_num" />
  </td>
    <td width="70%"><input name="Add" type="submit" value="Add" /></td>
  </tr>
    </form>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<?php


if(isset($_GET['Add']))
{

    $ref_num=$_GET['ref_num'];  
    $_SESSION['store'] = $ref_num;



}

array_push($_SESSION['mylist'],$_SESSION['store']);

echo '<pre>';
echo print_r($_SESSION['mylist']);
echo '</pre>';

?>

3 个解决方案

#1


1  

This is how you do it:

这是你如何做到的:

<?php

// Start PHP Session
session_start();

// Init List Array In Session
if (!isset($_SESSION['mylist'])) {
    $_SESSION['mylist'] = array();
}

// Handle Set Data Via GET
if (isset($_GET['ref_num']))
{
    // Append Data
    $ref_num = trim($_GET['ref_num']);
    if (!in_array($ref_num, $_SESSION['mylist'])) {
        $_SESSION['mylist'][] = $ref_num;
    }
}

?>
<form id="form1" name="form1" method="GET" action="">
    <input type="text" name="ref_num" id="ref_num" /> 
    <input name="Add" type="submit" value="Add" />
</form>
<?php

// Display Current Data (if Any)
if (isset($_SESSION['mylist'])) {
    echo 'Data In Session So Far:<br /><br /><pre>';
    print_r($_SESSION['mylist']);
    echo '</pre>';
}

?>

#2


0  

Dont know what you try there. You try to save the array within "$_SESSION", and then you want to use $_GET to obtain the values?

不知道你在那里尝试什么。您尝试将数组保存在“$ _SESSION”中,然后您想使用$ _GET来获取值?

You should take a closer look on $_GET, they are added in the url like this:

您应该仔细查看$ _GET,它们会添加到网址中,如下所示:

www.example.com?key=value

If you use $var = $_GET['key'] now, it will return "value".

如果你现在使用$ var = $ _GET ['key'],它将返回“value”。

#3


-1  

remove the

             echo '<pre>';
             echo print_r($_SESSION['mylist']);
            echo '</pre>';

echo from the middle line of content becuase print_r also print the records of array

回显来自中间行的内容,因为print_r还会打印数组的记录

#1


1  

This is how you do it:

这是你如何做到的:

<?php

// Start PHP Session
session_start();

// Init List Array In Session
if (!isset($_SESSION['mylist'])) {
    $_SESSION['mylist'] = array();
}

// Handle Set Data Via GET
if (isset($_GET['ref_num']))
{
    // Append Data
    $ref_num = trim($_GET['ref_num']);
    if (!in_array($ref_num, $_SESSION['mylist'])) {
        $_SESSION['mylist'][] = $ref_num;
    }
}

?>
<form id="form1" name="form1" method="GET" action="">
    <input type="text" name="ref_num" id="ref_num" /> 
    <input name="Add" type="submit" value="Add" />
</form>
<?php

// Display Current Data (if Any)
if (isset($_SESSION['mylist'])) {
    echo 'Data In Session So Far:<br /><br /><pre>';
    print_r($_SESSION['mylist']);
    echo '</pre>';
}

?>

#2


0  

Dont know what you try there. You try to save the array within "$_SESSION", and then you want to use $_GET to obtain the values?

不知道你在那里尝试什么。您尝试将数组保存在“$ _SESSION”中,然后您想使用$ _GET来获取值?

You should take a closer look on $_GET, they are added in the url like this:

您应该仔细查看$ _GET,它们会添加到网址中,如下所示:

www.example.com?key=value

If you use $var = $_GET['key'] now, it will return "value".

如果你现在使用$ var = $ _GET ['key'],它将返回“value”。

#3


-1  

remove the

             echo '<pre>';
             echo print_r($_SESSION['mylist']);
            echo '</pre>';

echo from the middle line of content becuase print_r also print the records of array

回显来自中间行的内容,因为print_r还会打印数组的记录