Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单仅将值而不是变量附加到URL_Php_Jquery_Forms_Url - Fatal编程技术网

Php 表单仅将值而不是变量附加到URL

Php 表单仅将值而不是变量附加到URL,php,jquery,forms,url,Php,Jquery,Forms,Url,我需要发送一个表单,其中只包含附加到URL的值,如下所示: // your form submit event handler function formSubmit() { var form = $('#consultatickets'); // build your url var url = 'http://thisistheurl.com/serv#search/' + getValues(form).join('/'); // red

我需要发送一个表单,其中只包含附加到URL的值,如下所示:

// your form submit event handler
function formSubmit() {
    var form = $('#consultatickets');
    // build your url
    var url = 'http://thisistheurl.com/serv#search/' + 
        getValues(form).join('/');

    // redirect to your new location
    location.href = url;
};

// function to get the values from the form elements
function getValues(form) {
    // get all form fields in the form
    var fields = $( "input, select", form);
    var values = [];
    // loop over the fields, add them to array
    jQuery.each( fields, function( field ) {
      values.push(encodeURI(field.val()));
    });
    return values;
}
值1/值2/值3/值4

我可以这样发送:

// your form submit event handler
function formSubmit() {
    var form = $('#consultatickets');
    // build your url
    var url = 'http://thisistheurl.com/serv#search/' + 
        getValues(form).join('/');

    // redirect to your new location
    location.href = url;
};

// function to get the values from the form elements
function getValues(form) {
    // get all form fields in the form
    var fields = $( "input, select", form);
    var values = [];
    // loop over the fields, add them to array
    jQuery.each( fields, function( field ) {
      values.push(encodeURI(field.val()));
    });
    return values;
}
?variable1=值&variable2=值&variable3=值&variable4=值搜索/

形式很简单

<form id="consultatickets" name="consultatickets" role="form" method="get" class="tickets-form" action="http://thisistheurl.com/serv#search/" target="_blank">

<div class="row">
    <div class="form-group col-md-12 col-sm-12">
        <label for="ciudadorigen" class="tickets-imputs">Ciudad de Origen</label>
        <select name="ciudadorigen" class="form-control ciudadorigen tickets-imputs" for="ciudadorigen">
            <option selected disabled>1</option>
            <option value="562">2</option>
            <option value="582">3</option>
        </select>                   
    </div>
</div>

<!-- Here goes the rest of the form -->

<a type="submit" class="btn btn-warning waves-effect btn-block" href="javascript:{}" onclick="document.getElementById('consultatickets').submit();">Buscar <i class="fa fa-search" aria-hidden="true"></i></a>
</div>


我不知道如何从变量中提取值并附加到URL。

未经测试,我不确定是否正确回答了您的问题,但您似乎在寻找类似以下内容:

// your form submit event handler
function formSubmit() {
    var form = $('#consultatickets');
    // build your url
    var url = 'http://thisistheurl.com/serv#search/' + 
        getValues(form).join('/');

    // redirect to your new location
    location.href = url;
};

// function to get the values from the form elements
function getValues(form) {
    // get all form fields in the form
    var fields = $( "input, select", form);
    var values = [];
    // loop over the fields, add them to array
    jQuery.each( fields, function( field ) {
      values.push(encodeURI(field.val()));
    });
    return values;
}

若您想触发带有标记的表单提交,只需将onclick属性更改为:onclick=formSubmit

如果顺序无关紧要,您可以通过序列化表单的值并将其附加到表单的action属性以构建最终url来实现这一点

<form id="consultatickets" name="consultatickets" role="form" method="get" class="tickets-form" action="http://thisistheurl.com/serv#search" target="_blank">

<div class="row">
    <div class="form-group col-md-12 col-sm-12">
        <label for="ciudadorigen" class="tickets-imputs">Ciudad de Origen</label>
        <select name="ciudadorigen" class="form-control ciudadorigen tickets-imputs" for="ciudadorigen">
            <option selected disabled>1</option>
            <option value="562">2</option>
            <option value="582">3</option>
        </select>
        <label for="campo_adicional">Campo adicional</label>
        <input id="campo_adicional" type="text" name="campo_adicional" />
    </div>
</div>
<input type="submit" value="search"/>
</form>

$("#consultatickets").on('submit',
function(e) {
  e.preventDefault();
  var values = $(this).serializeArray();
  var baseUrl = $(this).prop("action");
  $.each(values, function(i, v) {
    baseUrl += "/" + encodeURIComponent(v.value);
  });
  alert(baseUrl);
}
);

从你的例子中不清楚你指的是什么变量。如果希望捕获数据并将其重新映射到URL中,则需要在不使用HTML表单的默认函数的情况下执行此操作。到目前为止,您尝试了什么?变量是输入或选择的名称。HTML表单总是这样附加:?inputname=inputvalue&。我只需要这个值。我会尝试,但我认为你的方法是正确的。我不知道如何调用这个函数。在表单action中,我放置action=getValues;在submitonclick=document.getElementById'consultatickets'中提交;但是不起作用。@Estudio3551不,你做错了。我已经更新了我的答案-你确定你在使用jQuery吗?带有内联onclick的概念不是常见的jQuery方式,document.getElementById也不是。无论如何,您不能将javascript方法用作表单的操作。谢谢您的时间!!是的,订单很重要。我试试这个办法。谢谢,我不知道为什么,但它不起作用。小提琴很好用,我从那里抄写,但什么也没有。我改变了我的做事方式。非常感谢你。你如何修改你的代码?什么不工作意味着什么?这是一个有冲突的jquery问题。我将$更改为jQuery并处理警报。警报显示正确的URL,但我无法发送。它从不在表单中进行submt,而是停留在同一页面上,或者转到表单提交URL.Solved。我添加window.openbaseUrl,“u blank”;这就是把我发送到我想要的URL。