1
<script>
function user_pro(useron){
$.post("userprofile.php", { useron:useron } );
document.location.href="userprofile.php";
}
$(document).on('click','#userprofile',function(){
    var useron=$(this).data('id4');
    user_pro(useron);
});
</script>

i am trying to send data to the page "userprofile.php" through the jquery ajax .post() when i click a button with id 'userprofile'. i have stored the username of the user(which i retrieved from my database in data-id4 attribute ). i want to send this username to my userprofile.php page and display his profile(like dp,status and all...). the .data('id4') method is working fine as i am able to store data in the variable useron . but i am not able to send the data to userprofile.php . and i also simultaneous want to be directed to that page when i click the button with id="userprofile".

<a data-id4='".$row['username']."' id='userprofile' class='w3-btn w3-teal w3-hover-indigo'>profile</a>

this is the html element. the html is inside the echo tag of of a php page(that is why those quotes).

can somebody plz help me out here . thanks in advance :) .

  • i have now added the html element . – Rishav Mahapatra Apr 14 '16 at 06:04
  • What is the path of your `userprofile`? This should be `localhost/public/userprofile.php` – aldrin27 Apr 14 '16 at 06:06
  • You don't need ajax to do this. Take a look at [this question and answer](http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit). – larsAnders Apr 14 '16 at 06:07
  • yes but it in same folder so just wrote the name of the file. it is redirecting to that page , but in that page its showing undefined index 'useron' when i use it for an echo statement like echo $_POST['useron']; – Rishav Mahapatra Apr 14 '16 at 06:09
  • Your `$.post("userprofile.php", { useron:useron } );` and `document.location.href="userprofile.php";` are 2 different requests, so the `$_POST['useron']` from the 1st is not available for the 2nd. So your issue is that you want to redirect to `userprofile.php` while posting the `useron` value? – Sean Apr 14 '16 at 06:13
  • you can do something like this http://stackoverflow.com/a/10022098/689579 – Sean Apr 14 '16 at 06:26
  • thanks @larsAnders ,that page really helped , now its working ...i am uploading the code that worked . thanks everyone for helping. – Rishav Mahapatra Apr 14 '16 at 06:34

1 Answers1

2

this code worked ...

function post(path, parameters) {
var form = $('<form></form>');

form.attr("method", "post");
form.attr("action", path);

$.each(parameters, function(key, value) {
    var field = $('<input></input>');

    field.attr("type", "hidden");
    field.attr("name", key);
    field.attr("value", value);

    form.append(field);
});

// The form needs to be a part of the document in
// order for us to be able to submit it.
$(document.body).append(form);
form.submit();
}
$(document).on('click','#userprofile',function(){
    var useron=$(this).data('id4');
    post("userprofile.php",{useron:useron});
});

thanks everyone.