douzen1896
2018-01-30 13:29
浏览 70
已采纳

我是否需要在同一页面中为两个HTML表单添加前缀输入变量

I'm a bit confused of do I need to prefix form input variables with i.e. ('car_', or 'bike_' corresponding to 'car_make', 'bike_make') or can I use same template for both forms without prefixing variables. And if so, do I still need to prefix the 'submit' field, or having different form name is enough to avoid data collision.

I have these two HTML forms on the same page:

<form action="" name="car_search_form" method="POST">
<input type="hidden" name="make" value="Audi" />
<input type="submit" name="car_do_search" value="Search Car" />
</form>
<form action="" name="bike_search_form" method="POST">
<input type="hidden" name="make" value="Schwinn" />
<input type="submit" name="bike_do_search" value="Search Bike" />
</form>

So, in the end I want to get correct value for $validCarMake via this code:

if(isset($_POST['car_do_search']))
{
     $paramCarMake = isset($_POST['make']) ? sanitize_text_field($_POST['make']) : '';
     $validCarMake = esc_sql($paramCarMake); // For SQL queries only
     // TODO: Add data saving to database
}

Also, I want to understand the decision process of PHP engine. How it does deside which variable to choose - how it know on which button I clicked, and why not it does just submit all forms on the page? Also, if there would be any difference if I'd use "GET" method instead of "POST" besides that the one does put the variable values to URL? And how does the GET case it would process attached image to form submit then (as the maximum URL lenght is 255 chars as I know, and i.e. JPEG 100 kiB image contains thousands of chars. I'm asking this, because I also want to allow not just have that search on site's home page, but also allow to make search from a separate website's some kind of widget.

And the last question - if the HTML form processing differs somehow in PHP 7.X compared to PHP 5.X (i.e. PHP 5.4). I means does it caches somewhere the data, does it sends over the internet the attached images of the both forms and consumes network and server data, or it submit's only the data of the form on which I clicked the button.

图片转代码服务由CSDN问答提供 功能建议

我有点困惑我是否需要为表单输入变量添加前缀ie(' car_ < / code>',或者' bike _ '对应于' car_make ',' bike_make ')或者我可以为这两种形式使用相同的模板 前缀变量。 如果是这样,我是否还需要在“提交”字段前加上前缀,或者使用不同的表单名称足以避免数据冲突。

我在同一页面上有这两个HTML表单:

 &lt; form action =“”name =“car_search_form”method =“POST”&gt; 
&lt; input type =“hidden”name =“make”value =“ 奥迪“/&gt; 
&lt; input type =”submit“name =”car_do_search“value =”搜索汽车“/&gt; 
&lt; / form&gt; 
&lt; form action =”“name =”bike_search_form“method =”  POST“&gt; 
&lt; input type =”hidden“name =”make“value =”Schwinn“/&gt; 
&lt; input type =”submit“name =”bike_do_search“value =”Search Bike“/&gt; \  n&lt; / form&gt; 
   
 
 

所以,最后我想通过以下代码获取 $ validCarMake 的正确值:

  if(isset($ _ POST ['car_do_search']))
 {
 $ paramCarMake = isset($ _ POST ['make'])?  sanitize_text_field($ _ POST ['make']):''; 
 $ validCarMake = esc_sql($ paramCarMake);  //仅适用于SQL查询
 // TODO:将数据保存到数据库
} 
   
 
 

另外,我想了解PHP引擎的决策过程。 它如何确定选择哪个变量 - 它如何知道我点击了哪个按钮,为什么不在页面上提交所有表单? 另外,如果我使用“GET”方法而不是“POST”除了那个将变量值放到URL之外会有什么区别吗? 然后GET案例如何处理附加图像到表单提交(因为我知道最大URL长度是255个字符,即JPEG 100 kiB图像包含数千个字符。我问这个,因为我也想 不仅允许在网站的主页上进行搜索,还允许从单独的网站搜索某种小部件。

最后一个问题 - 如果HTML表单处理在某种程度上有所不同 PHP 7.X与PHP 5.X(即PHP 5.4)相比。我的意思是它在某个地方缓存数据,它是否通过互联网发送两种形式的附加图像并消耗网络和服务器数据,或者它只提交 我点击按钮的表格数据。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxiajia6720 2018-01-30 13:38
    已采纳

    As long as you keep the 2 input requests separate having the post arg "make" is completely fine. If you send the args in the same request the 2nd will override the first since it was last set.

    As for how php decides on what is first it uses what is called order of precedence. This means what it comes to first it executes first unless explicitly told not to.

    点赞 打赏 评论
  • dongmei9508 2018-01-30 13:44

    I want to understand the decision process of PHP engine. How it does decide which variable to choose - how it know on which button I clicked, and why not it does just submit all forms on the page?

    When you click on a button, php will take the <button name="value"> value assigned in the name property of the input field or button clicked. This is how it can decide what's the form to submit. Consider that if you have two forms with the same name assigned to the submit button, the first one will override the second and php will only submit one form. This is because php execute operations with a logical order.

    点赞 打赏 评论

相关推荐