0

I use codeigniter and i want make uniqid code with php as following code(by random_string()):

function coding(){
    echo random_string('unique');
}

I want use result top function in a, jquery function (for use in js code), i in jquery code try as this, but this output in alert(coding()) is "undefined". How can fix it?

function coding(){
   $.ajax({
        type: "POST",
        dataType: "text",
        url: "coding",
        cache: false,
        success: function (data) {
            return data;
        }
   })
}
alert(coding()) // in here output is  "undefined" !!!?

5 Answers 5

2
function coding(){
   var temp;
   $.ajax({
        type: "POST",
        dataType: "text",
        url: coding,
        cache: false,
        async: false,
        success: function (data) {
            temp = data;
        }
   })
   return temp;
}
alert(coding());

You will receive an [object XMLDocument].

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

3 Comments

Are you sure this will work? AJAX call in asynchronous, so success is called when the function coding() already returned temp. My guess is you will always get undefined.
"async: false" makes the call synchronous
oops, missed that... true. :) OP should take care though - this means the function will "hang" for the duration of the call. It would be better if OP changed the way he calls the function and use AJAX properly (asynchronously).
1

It is becuse coding function is asynchronous. Use follow code:

function coding(){
   $.ajax({
        type: "POST",
        dataType: "text",
        url: coding,
        cache: false,
        success: function (data) {
            alert(data);
        }
   })
}
coding();

2 Comments

I don't want alert, in above code it have output, but man want use return it as append in other js code, this just done alert result php code. how is it?
@RamilAmr I vote up for you because you understand his problem
1
function coding(){
   $.ajax({
        type: "POST",
        dataType: "text",
        url: "coding",
        cache: false,
        success: function (data) {
            $("#yourDiv").html(data);
        }
   })
}

You can assign value to another element like div, p etc.

4 Comments

@Kate Wintz Alert? I append html to a div, instead of html() you can use append(data);
@KateWintz are you copy your comment without read my soulution?
I mean is the same, i want use of name function as "coding()" for echo return this function in other js code. but your code append return in a selector.
@KateWintz sorry, I think you can't, because this is asynchronous call. You should to include your js to another page
0

Well, I'm assuming that the coding variable you are passing at the parameter url is a reference to a method in your CI controller.

If that's the case, you just need to wait from the success callback:

$.ajax({
        type: "POST",
        dataType: "text",
        url: coding,
        cache: false,
        success: function (data) {
            alert(data);
        }
   })

1 Comment

I don't want alert, in above code it have output, but man want use return it as append in other js code, this just done alert result php code. how is it?
0

Ok, you should use callback function like this:

function coding(func){
   $.ajax({
        type: "POST",
        dataType: "text",
        url: coding,
        cache: false,
        success: function (data) {
            func(data);
        }
   })
}
coding(function(data)
{
   alert(data);
});

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.