dongmi1221 2015-01-02 00:52
浏览 41

将值传递给服务器[重复]

This question already has an answer here:

I have been trying to find methods to pass variables from client to server What is making it difficult for me is:

  1. I have an unknown number of variables( The program that I am working on pass different number of variables/arrays to server side depending on what the user is doing )
  2. The user could be using any character including & and = therefore if I passed the variables as multiple/one GET variable/s the user could pass = and & which will affect the GET variables

I am looking for methods ( a solution ) to pass variables, preferably arrays, to server side ( like PHP )

Appreciate your time and help :)

</div>
  • 写回答

2条回答 默认 最新

  • dongzhunqiu4841 2015-01-02 00:56
    关注

    To pass arrays to the server, you would do something like this (note the [] in the name, this signifies an array):

    <input type="checkbox" name="myCheckBox[]" value="One" />
    <input type="checkbox" name="myCheckBox[]" value="Two" />
    <input type="checkbox" name="myCheckBox[]" value="Three" />
    <input type="checkbox" name="myCheckBox[]" value="Four" />
    

    Then in php you would do something like this:

    foreach($_POST['myCheckBox'] as $value){
        echo "<p>$value</p>";
    }
    

    You would then get output that looks like this (assuming all but Two was selected):

    One
    Three
    Four
    

    So assuming you are not using javascript to pass the data, the browser should automatically encode the data. If you are using javascript you can pass the data to the javascript function escape() and it will escape/encode the data to be passed to the server.

    Also for jquery you can use the serialize() operation:

    $.ajax(
         url: "/to/file.php",
         data: $("#my_form").serialize(), // <form id="my_form">
         success: function(){
             // Do some stuff
         }
    );
    

    Here is another way to pass data:

    <input type="text" name="names[0]['name']['first']" />
    <input type="text" name="names[0]['name']['last']" />
    <input type="radio" name="names[0]['gender']" value="male" />
    <input type="radio" name="names[0]['gender']" value="female" />
    
    <input type="text" name="names[1]['name']['first']" />
    <input type="text" name="names[1]['name']['first']" />
    <input type="radio" name="names[1]['gender']" value="male" />
    <input type="radio" name="names[1]['gender']" value="female" />
    
    <input type="text" name="names[2]['name']['first']" />
    <input type="text" name="names[2]['name']['first']" />
    <input type="radio" name="names[2]['gender']" value="male" />
    <input type="radio" name="names[2]['gender']" value="female" />
    
    <input type="text" name="names[3]['name']['first']" />
    <input type="text" name="names[3]['name']['first']" />
    <input type="radio" name="names[3]['gender']" value="male" />
    <input type="radio" name="names[3]['gender']" value="female" />
    

    The in the php you would do this for example:

    foreach($_POST['names'] as $name){
        echo "<p>" . $name["name"]["first"] . " is a " . $name["gender"] . "</p>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler