Dee
Dee

Reputation: 49

Javascript id as parameter in href

Second image ,First image I have a set of links, which will open a pop-up form.
When the link is clicked,I want to send a parameter to the form and then use it on form submission.
I'm able to set the value to be passed as id of <a> tag. Can I send to further?

<div> <span>Chapter $i:</span>
  <a href='$viewlink '>View</a><span class='status'>Status:$status </span>
  <a href=$reqlink id=$i data-rel='popup' class='ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left'>Request Access</a></div><br/>";
<form method="post" action=" " id="myPopup" data-role="popup" style="padding:10px">

  <h3>Enter your details</h3>
  <input type="text" name="name" id="name" placeholder="Name" required>
  <input type="email" name="email" id="email" placeholder="Email" required>
  <input type="date" name="date" id="date" placeholder="Intended completion date" required>
  <input type="submit" data-inline="true" value="Submit" name='submit'>

</form>

Is it possible to do in javascript? How to do it?

Upvotes: 2

Views: 118

Answers (2)

benomatis
benomatis

Reputation: 5633

Option #1:

Set up hidden inputs, and send the values to them when clicking the link. You can then get these on the other end where the form is sent.

(Note: in my code examples I'm explicitly using PHP as that's where you seem to have copied your code snipped from)

echo "<a href='$viewlink' onclick='$(\'#viewlink\').val(1);'>View</a><span class='status'>Status:$status </span>

<!-- Do the following inside the form -->

<input type='hidden' name='viewlink' id='viewlink' value='0' />";

And on the PHP receiving end you can do this:

if ($_POST['viewlink'] == 1) {
    // do stuff
}

Option #2:

Alternatively you could send the data to a javascript array, prevent posting on submit of the form, take care of adding the array to the form action as query string, then explicitly send the form.

echo "<a href='$viewlink' onclick='linkClicked('viewlink');'>View</a><span class='status'>Status:$status </span>

This is what you'd do in your javascript file:

var queryString = [];

function linkClicked (type) {
    queryString[type] = 1;
}

$("#myPopup").submit(function(event) {
    event.preventDefault();
    $(this).attr('action', $(location).attr('host') + $.param(queryString));
    $(this).submit();
});

And on the PHP receiving end you can do the following (note the $_POST from above has changed to $_GET):

if ($_GET['viewlink'] == 1) {
    // do stuff
}

Upvotes: 2

WellJhim
WellJhim

Reputation: 26

try this..

<a id = 'yourid' class = 'mybtn'>click me..</a>

<form id = 'myform'>
  ....
</form>

Jquery

$(document).ready(function(){
    $('.mybtn').click(function(){
        var id = $(this).attr('id');
        var SubmitForm = $("#myform").serializeArray();
        $.post("somepage.php",
        {
         SubmitForm:SubmitForm,
         ID:id
        },
        function(res){
            alert(res);//your result..
        });
});

Upvotes: 0

Related Questions