0

I am using Ajax and jQuery in yii.

I have a view its url is like that.

http://localhost/UnderstandQuran/index.php?r=Verse/topicverse&topic_id=1

Now there are some radio buttons group. When i click on it, it will be called by jquery.

My View Code.

<?php $i= 0;
foreach($data->verse_lang as $vtlanguage) {
$i = $i+1 ;
if($i === 1) { ?>
<input type="radio" name="<?php echo "tran".$data->id ?>" class="translanguage" langid="<?php echo $vtlanguage['id']; ?>" verseid="<?php echo $data->id ?>" checked="checked"><?php echo $vtlanguage['language_name']; ?></input>
<?php } else { ?>
<input type="radio" name="<?php echo "tran".$data->id ?>" class="translanguage" langid="<?php echo $vtlanguage['id']; ?>" verseid="<?php echo $data->id ?>"><?php echo $vtlanguage['language_name']; ?></input>
<?php } } ?>

My JQuery Code

$("input[type='radio'][class='translanguage']").click(function ()
{
    var siteUrl = document.URL;
    var verseid = $(this).attr("verseid");
    var langid = $(this).attr("langid");
    var vname = $(this).attr("name");

    $.ajax(
    {
        type: "POST",
        url: siteUrl + "/singleverse",
        data:
        {
            verseid: verseid,
            langid: langid
        },
        success: function (result)
        {
            alert(result);
        }
    });
});

Through Ajax, i am sending it to the controller/Controller Method.

My Controller Method

public function actionSingleverse()
{
    $verseid = $_POST['verseid'];
    $langid = $_POST['langid'];
    echo $verseid." ".$langid;
}

My Question

Now through all of my code. It should be giving the alert box with two different integer value, but it is displaying all the html code of the page. So how i can solve this issue. I have checked in console.log, but gives 505 error.

My all this code is working fine when the above URL is like that.

http://localhost/UnderstandQuran/index.php?r=Verse

Please help me, what and where is the problem.

Thanks.

2
  • Is't there problem with url? it seems to be http://localhost/UnderstandQuran/index.php?r=Verse/topicverse&topic_id=1 in document.URL so siteUrl var in javascript is http://localhost/UnderstandQuran/index.php?r=Verse/topicverse&topic_id=1/singleverse Commented Dec 11, 2013 at 13:04
  • yes, there is a URL problem here. Commented Dec 16, 2013 at 7:57

2 Answers 2

1

Paste your url in the following format am sure it will work

  url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/controllername/singleverse',
Sign up to request clarification or add additional context in comments.

Comments

0

May be there will be some problem in this place

var siteUrl = document.URL;

The Site Url can also be got in javascript like this

var siteUrl = <?php echo Yii::app()->getBaseUrl() ?>;

May be this could help you

1 Comment

Note: json_encode() or at least quotes should probably be used in the 2nd snippet as JavaScript doesn't support URLs as literals.

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.