3

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');

3 Answers 3

3

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();
});
Sign up to request clarification or add additional context in comments.

Comments

2

you could also do something like this, which is an alternative syntax

$('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();
});

in your controller $test_var= $this->input->post('dataString');

or just like in vanilla php

$test_var = $_POST['dataString'];

Comments

0

use data option like this data: { name: "John", location: "Boston" } for more details plz check http://api.jquery.com/jQuery.ajax/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.