0

I have used an Input type="date" field for accepting date value from the user in my application. But I have to post value of date named as "sdate" into the address used in the action method as shown below. I want to post value of Input type date field into action of Form Post method.

I have stored those values via AJAX into variables but How to use those AJAX variables into the action of

My written view code is as below:

<form method="post" id="report" class="live" target="_blank" action="http://wcidevapps.com/salescentral/jm2/troute.php?custnum=<?php echo $KUNNR?>&staffid='+staffid+'&rdate='+sdate+'">

<fieldset><legend>Today's Tasks</legend></fieldset>

<div class="span5 well">
    <div class="form-inline">
        <label class="control-label"><b> For </b> </label>
            <input type="text" name="stech" id="stech"/>
    </div><br/><br/>

    <div class="form-inline">
        <label class="control-label"><b> On </b> </label>
            <input type="date" class="datefield" name="sdate" id="sdate"/>
    </div><br/><br/>

    <button type="submit" name="open" class="btn btn-primary offset4"><i class="icon-file"></i> Open</button>
</div>

</form>



<script type="text/javascript">

var onFilter = function () {
        var obj = {
            sdate: $("#sdate").val(),
            stech: $("#stech").val()
        };
        $.ajax({
            method: 'POST',
            dataType: "JSON",
            contentType: "application/json; charset=utf-8",
            url: root + 'standard_projects/?json',
            data: obj,
            success: function (d) {
            sdate=d.sdate,
            staffid=d.staffid
            }
        });
    }
    $("#sdate").live("change", onFilter);
    $("#stech").live("change",onFilter);
</script>

How can I use those AJAX variables ? sdate & staffid

9
  • are sdate & staffid defined outside the function? Commented Dec 20, 2012 at 6:24
  • no those are not defined outsside of function Commented Dec 20, 2012 at 6:26
  • 1
    Can you clarify what you mean by "use" these variables? What do you want to do with them? Commented Dec 20, 2012 at 6:27
  • I just want to send the request to some site for some documents for that I need to give verified staffid Which I am getting into AJAX variable staffid after successful server response that I want to post to the address used in the action of Form tag .. Commented Dec 20, 2012 at 6:30
  • if they are not defined outside the function ie, local to the function only and you want to use it, then declare it outside your function like a var sdate, staffid; Commented Dec 20, 2012 at 6:35

3 Answers 3

2

You cannot use ajax variable outside ajax. instead of that Declare variable from start of script

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are missing the document ready handler and all seems fine. Well use of live is deprecated in latest jQuery versions so instead you can use .on() handler for it.

and you are missing some ';'

from jQuery documentation:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

try this and see if helps:

<script type="text/javascript">

var onFilter = function () {
        var obj = {
            sdate: $("#sdate").val(),
            stech: $("#stech").val()
        };
        $.ajax({
            method: 'POST',
            dataType: "JSON",
            contentType: "application/json; charset=utf-8",
            url: root + 'standard_projects/?json',
            data: obj,
            success: function (d) {
            sdate=d.sdate; //<--------------missing ';'
            staffid=d.staffid; //<----------missing ';'
            $('form#report').attr("action", "http://wcidevapps.com/salescentral/jm2/troute.php?custnum="+<?php echo $KUNNR?>+"&staffid=" + staffid + '&rdate=' + sdate + '"');
            }
        });
    }; //<----------------------------------missing ';'
    $(function(){
        $(document).on("change", "#sdate", onFilter);
        $(document).on("change", "#stech", onFilter);
    });
</script>

6 Comments

I want the answer of how to use my staffid & sdate variables of AJAX into same Page in 'action' attribute of <form> ?
@Rattatolae see the updated line in the code. where we are attaching the variable to the action of the form.
It returns back to same page address not to the address given in action? how can It be done for opening that Link address ?
the address in action isn't used by <form> & it opens same page Address on which it is right now
you mean your form is not submitting to the action url.
|
0

Just use it in your success function

<form method="post" id="report" class="live" target="_blank" action="http://wcidevapps.com/salescentral/jm2/troute.php?custnum=<?php echo $KUNNR?>&">

//...
var report=$('#report), 
    url=report.attr('action'), 
    useStaff = function(staffid, sdate){
        report.attr('action', url+'staffid='+staffid+'&rdate='+sdate);
        //submit report or so...
    };
$.ajax({
        method: 'POST',
        dataType: "JSON",
        contentType: "application/json; charset=utf-8",
        url: root + 'standard_projects/?json',
        data: obj,
        success: function (d) {
            useStaff(d.staffid, d.sdate);
        }
    });

3 Comments

But I want to use those staffid & sdate variables in my form tag action having some address ....
But It is not concatenating the address value ?
It is report.attr('action', url+'staffid='+staffid+'&rdate='+sdate);

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.