简体   繁体   English

用JavaScript发送两个参数/数据

[英]Sending two parameters/data in javascript

I have this code which posts data to a page and returns data. 我有这段代码将数据发布到页面并返回数据。 This is done through javascript. 这是通过javascript完成的。 I want to post to different pieces of data from the form, the url which is already there and a hidden user field. 我想从表单,已经存在的url和隐藏的用户字段中发布不同的数据。 Here is the code; 这是代码;

function go(url) { 函数go(url){

$.post('urlin.php', { url : url }, function(data ) {

if (data== 'error_no_url'){

    $('#message').html('<p>Try entering an actual URL!</p>');


    } else if (data == 'error_roll') {

    window.open('http://1227.com');
    $('#message').html('<p>ROFL</p>');

} else if (data == 'error_invalid_url') {

    $('#message').html('<p>Not a valid URL</p>');

} else if (data == 'error_is_min') {


$('#message').html('<p>Already a short URL, it can\'t get any shorter!</p>');   

} else {

    $('#url').val(data);
    $('#url').select();
    $('#message').html('<p> URL Shortened </p>');       
}

});

} }

and; 和;

<p><input type="text" size="85" placeholder="Enter a URL to shorten." name="url" id="url" onkeydown="if (event.keyCode == 13 || event.which == 13) { go($('#url').val()); }" /><input type="hidden" name="user" value="<?php echo $_SESSION['auth']; ?>"><input type="submit" class="btn green" value="Shorten" border="0" onclick="go($('#url').val());" />

At the moment it only posts the url. 目前,它仅发布网址。 How can i get it to post the user aswell. 我如何才能发布用户。

I would maybe do something like this... 我可能会做这样的事情...

 <input type="submit" onclick="javascript:go()">

and your Javascript/JQuery function... 和您的Javascript / JQuery函数...

function go()
{
   var url = $('#url').val();
   var user = $('input[name="user"]').val();
   $.post('urlin.php', { url: url, user: user }, function(data ) {
        // handle post
   });
}

You can just modify your go function to accept an extra parameter: 您可以修改go函数以接受额外的参数:

function go(url, user) {
    ...
}

In your input fields, add an id to the hidden field, and modify go function call to something like go($('#url').val(), $('#user').val()); 在您的输入字段中,向隐藏字段添加一个id,然后将go函数调用修改为go($('#url').val(), $('#user').val()); :

<input type="text" size="85" placeholder="Enter a URL to shorten." name="url" id="url" onkeydown="if (event.keyCode == 13 || event.which == 13) { go($('#url').val(), $('#user').val()); }" />
<input type="hidden" name="user" id="user" value="<?php echo $_SESSION['auth']; ?>">
<input type="submit" class="btn green" value="Shorten" border="0" onclick="go($('#url').val(), $('#user').val());" />

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

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