Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Problem with underscores in URL
#1

[eluser]Đaяк Đaηтє[/eluser]
I have an app where I use codeigniter, I send data from a html form to a database, but when I save a string like this "this is my string", in database I see "this_is_my_string" any idea why?
#2

[eluser]umefarooq[/eluser]
before saving into database you can just replace your empty space with _ as

Code:
$string = "this is my string";
$string = str_replace(' ','_',$string);

it will work, or you can use CI

Code:
$title = "What's wrong with CSS?";

$url_title = url_title($title, 'underscore');

it will work check in url helper

http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]Đaяк Đaηтє[/eluser]
Maybe, I have not explained the problem correctly. I have a form, this form can be used to save information in the database, in this step, I´m making testing with this form, simply adding information and saving this information into a table in database.

For example if I need to save the name of John Teser, I write John Teser in the TextField, I save this string and then when I check in the database, I see John_Teser, my question is, why CodeIgniter adds "_" character in every space in a String? I cannot scape the "_" character because if i need to save an email address, some emails addresses had "_" character.

Anyone have an idea? Do any way to modify this behavior?
#4

[eluser]TheFuzzy0ne[/eluser]
If you post some code, we might be able to give you some pointers.
#5

[eluser]Đaяк Đaηтє[/eluser]
OK, this is the form VIEW:
Code:
<style type="text/css">
    form{ margin:0; width:400px;}
</style>
<center>
&lt;form id="frmSchool" name="frmSchool"&gt;
  <table border="0" align="center" cellpadding="1" cellspacing="1">
    <tr>
      <td align="left">School´s Name</td>
      </tr>
    <tr>
      <td><label>
        &lt;input name="txtSchool" type="text" id="txtSchool" size="60" maxlength="80" class="input_text" /&gt;
      </label></td>
      </tr>
    <tr>
      <td align="center">&nbsp;</td>
    </tr>
    <tr>
      <td align="center">&lt;input type="button" class="button"&gt;
        &lt;input type="reset" class="button" name="btnCancel" id="btnCancel" value="Cancel" /&gt;&lt;/td>
    </tr>
  </table>
&lt;/form&gt;
&lt;?php
if(isset($resp))
{
    echo $resp;
}
?&gt;
</center>

This is the CONTROLLER
Code:
&lt;?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MasterController extends Controller
{
    function __construct()
    {
        parent::Controller();
        session_start();
    }
    
    function index()
    {
        $this->load->view('home_page');        
    }    
    
    function save_school($school)
    {
        $this->load->model('MasterModel');
        $data['result']=$this->MasterModel->save_data($school);
        $this->load->vars($data);
        $this->load->view('school_page');
    }
}
?&gt;

This is the MODEL:
Code:
&lt;?php
class MasterModel extends Model
{
    function __construct()
    {
        parent::Model();
    }

    function save_data($school)
    {
        $sql = "INSERT INTO schools (pk_school, name_school) VALUES ('', ".$this->db->escape($school).")";
        $this->db->query($sql);
        return $this->db->affected_rows();                
    }
}
?&gt;

I use jquery and Ajax that's is the reason because in the form you see onclick="saveSchool();" but you can put the controller in the form to work with this example...
#6

[eluser]TheFuzzy0ne[/eluser]
Everything looks normal to me. Can you confirm that the data hasn't got underscores in it before it actually goes into the database? By that, I mean on the server end. I suspect the server might be modifying the post array.
#7

[eluser]Đaяк Đaηтє[/eluser]
When I invoke a controller i do this:

Controller/Function/FirstParameter/SecondParameter/...

It is really easy, but I don´t know what happend when I sent a String like this "First Parameter" then le url looks like this:

Controller/Function/First Parameter/Second Parameter/... or
Controller/Function/First%20Parameter/Second%20Parameter/...

I´m not sure what can I do to allow white spaces in url parameters, because CI return First_Parameter/Second_Parameter why, CI puts underscores when white spaces appears...
#8

[eluser]Johan André[/eluser]
thats normal behavior of CI.
Might help to url_encode() the string before putting it in the url.
#9

[eluser]Đaяк Đaηтє[/eluser]
OK, It is a normal behavior in CI. I use JQuery to make ajax request, I make an ajax request like this:

Code:
function saveRecord()
{
    var data = $('#txtData').val();
    

    
    $.ajax({
          url        :     "MasterController/save_record/"+Base64.encode(data),
          type    :     "POST",
          cache    :     false,
        beforeSend: function()
                {
                    $('#div_result').attr('innerHTML', "Saving...");
                },
          success    :     function(result)
                {
                    $("#div_result").unmask();
                    $('#div_result').attr('innerHTML', result);
                  },
        error    :    function()
                {
                    alert('Error, try again.');
                }
    });    
}

I needed encode the string before send it to controller, my question now is, Are there another way to send the parameters, to function's controller, using the "data" attribute, in jquery ajax object?.

By the way this is the JS pseudo class to enconde any string:
Code:
*  http://www.webtoolkit.info/
*  var data = Base64.encode(document.getElementById('data').value);
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i &lt; input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 &gt;&gt; 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i &lt; input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 &lt;&lt; 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


Greetings and thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB