jQuery AJAX call to PHP Controller

ajax, codeigniter, html, jquery, php

Solution

Try using a name=value pair:

$('a.test').click(function (event) {


    dataString = $(this).attr('name'); 
        $.ajax({
        type:"POST",
        url:"<?php echo base_url(); ?>controller_name/",
        data:'dataString='+dataString,
        success:function (data) {
        alert('test');
        }

    });
    event.preventDefault();
});

Problem

I am calling a controller (Codeigniter) through jQuery. My dataString variable contains a simple string, which I'm trying to pass through to my controller, so I can pass it into the model. However, I'm getting an error that indicates that my $test_var is undefined. What am I doing wrong? ``` $('a.test').click(function (event) { dataString = $(this).attr('name'); $.ajax({ type:"POST", url:"<?php echo base_url(); ?>controller_name/", data:dataString, success:function (data) { alert('test'); } }); event.preventDefault(); }); ``` controller $test_var= $this->input->post('dataString');

Original source