简体   繁体   English

传递变量的更好方法?

[英]Better way to pass variables?

I have form which generates content in a textarea box, and currently I'm passing the forms options as following: 我有一个在textarea框中生成内容的表单,当前我正在传递表单选项,如下所示:

function button_click() {
    var columns = $('#form_id').val();
    var url='generate.php?';        
    url+='&form_id='+encodeURIComponent(form_id);
    $.ajax({
    url: url,
    beforeSend: function (xhr) {
        xhr.overrideMimeType("application/json; charset=x-user-defined");
    }
    })

Then on the php page I have a request: 然后在php页面上,我有一个请求:

$form_id= $_REQUEST['form_id'];

Then I can use the variable how I wish in php. 然后,我可以按照自己的意愿在php中使用变量。

It seems a bit redundant. 似乎有点多余。

How can I condense this, if I make the form all php instead of using all ajax/json would that make a big difference? 如果我将表格全部设为php而不是全部使用ajax / json,那我该如何浓缩呢?

If I understood your question correctly I would say that it is impossible to do what you want. 如果我正确理解了您的问题,我会说这是不可能做的。 The problem is that PHP script is executed on the server side, and javascript and form are handled by client side (browser). 问题是PHP脚本在服务器端执行,而javascript和表单由客户端(浏览器)处理。

So in general there is no way to get direct access from PHP to variables (elements) of your form/javascript. 因此,通常无法从PHP直接访问表单/ javascript的变量(元素)。 You can only pass these variables from the form by GET/POST HTTP request to server where you can handle them by your PHP script. 您只能通过GET / POST HTTP请求将这些变量从表单传递到服务器,然后可以通过PHP脚本处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM