使用输入值作为数组变量

时间:2015-08-20 06:12:01

标签: php

我对php完全不熟悉。我试图将输入字段的值回显到一个数组中,但它似乎不起作用。例如,将hidden-input的值作为数组中origin的值进行回显。我怎样才能做到这一点?

<form method="post">
    <!-- Set type -> Hidden, if you want to make that input field hidden.
You really should use better "names" for the input fields -->

// I populate the value with jQuery  //

    <input id="hidden-input" type="hidden" name="from" value="">

</form>
<?php 


    $params = array(
        'origin'        =>  $_post['from'],
        'destination'   => um_user('postal_zip_code'),
        'sensor'        => 'true',
        'units'         => 'imperial'
    );


    $params_string='';
    // Join parameters into URL string
    foreach($params as $var => $val){
        $params_string .= '&' . $var . '=' . urlencode($val);  
    }

    // Request URL
    $url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');

    // Make our API request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);

    // Parse the JSON response
    $directions = json_decode($return);

    //echo"<pre>";
    //print_r($directions);

    // Show the total distance
    echo '<p><strong>Total distance:</strong> ' . $directions->routes[0]->legs[0]->distance->text . '</p>';


    ?>
<div id class="distance"></div>

插入值的jQuery如下所示,它完美无缺。它按照假设插入它,但php不会将它传递给数组

 $(document).ready(function() {
 var someName = $('.um-field-postal_zip_code .um-field-value').text();
 $("input#hidden-input").attr("value", someName);

});

3 个答案:

答案 0 :(得分:0)

<form method="post">
<!-- Set type -> Hidden, if you want to make that input field hidden.
You really should use better "names" for the input fields -->

<input id="hidden-input" type="hidden" name="from" value="">
</form>

<?php 

$params = array(
    'origin'        =>  $_POST['from'], // <- Now it should be empty, because nothing is inside
    'destination'   => um_user('postal_zip_code'),
    'sensor'        => 'true', // If you write 'false' it's still true, because the string is filled. Please use correct bools like true / false without the quotes // 'sensor' => true 
    'units'         => 'imperial'
); 
?>

您还需要“发送”或“激活”表单。

<input type="submit" name="sendForm" value="Send Form"/>

所以输入表格如下:

<form method="post">
<!-- Set type -> Hidden, if you want to make that input field hidden.
You really should use better "names" for the input fields -->

<input id="hidden-input" type="hidden" name="from" value="">
<input type="submit" name="sendForm" value="Send Form"/>
</form>

如果您需要更多帮助,请与我们联系!

答案 1 :(得分:0)

HTML

<form method="post" action="yourpage.php">
   <input id="hidden-input" name="from" value="">
   <input type="submit" value="Submit" name="submit_button">
</form>

PHP

<?php 
//if your form is submitted fill the array
if(isset($_POST['submit_button'])){
   $params = array(
      'origin'        =>  $_POST['from'],
      'destination'   => 'postal_zip_code',
      'sensor'        => 'true',
      'units'         => 'imperial'
   );

   //print array 
   foreach($params as $index=>$value){
     print $index." :".$value;
   }
}
?>

答案 2 :(得分:0)

如果不刷新页面,则无法从表单中获取数据。实际上,您正在尝试从来自jquery的隐藏输入中获取数据。

首先删除表单html并进行ajax调用

您需要设置ajax

$(document).ready(function() {
 var someName = $('.um-field-postal_zip_code .um-field-value').text();

if(!someName==''){//check somename is blank or not
//make ajax call
   $.ajax({url: "url of your php code", 
    data : 'someName',
    type : 'post',
    success: function(result){

        }});
}
});  

在php文件中使用$_post

获取数据
echo $data = $_post['data'];//this is your origin

现在继续使用您的代码

$params = array(
        'origin'        =>  $data,
        'destination'   => um_user('postal_zip_code'),
        'sensor'        => 'true',
        'units'         => 'imperial'
    );
$params_string='';
    // Join parameters into URL string
    foreach($params as $var => $val){
        $params_string .= '&' . $var . '=' . urlencode($val);  
    }

// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');

// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);

// Parse the JSON response
$directions = json_decode($return);

//echo"<pre>";
//print_r($directions);

// Show the total distance
echo '<p><strong>Total distance:</strong> ' . $directions->routes[0]->legs[0]->distance->text . '</p>';