1

I have this code which posts data to a page and returns data. This is done through javascript. I want to post to different pieces of data from the form, the url which is already there and a hidden user field. Here is the code;

function 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.

2 Answers 2

3

I would maybe do something like this...

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

and your Javascript/JQuery function...

function go()
{
   var url = $('#url').val();
   var user = $('input[name="user"]').val();
   $.post('urlin.php', { url: url, user: user }, function(data ) {
        // handle post
   });
}
Sign up to request clarification or add additional context in comments.

2 Comments

I just need to send an extra parameter, how can i incorporate this into my existing code and the field i am trying to send is hidden
Not sure I understand your comment. The above grabs the values from the #url element and the hidden element. These are then posted to the urlin.php script. See link
2

You can just modify your go function to accept an extra parameter:

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());:

<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());" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.