Welcome Guest, Not a member yet? Register   Sign In
Jquery with CI
#1

[eluser]finord[/eluser]
Hello, i have problem with this script. I try to send data usng ajax( with jQuery)
, and insert to bd (mysql).

But i dont know why dont work ajax, the go directly to the controller url.






Controller:
Code:
<?

class fadd extends Controller
{
function sendt()
         {        
    $this->load->helper('form');
            $this->load->view('form');
    }
         }
?>



Model:
Code:
<?  

class add extends Controller
    
     {  
        function add()
         {
           parent::Controller();
          }
          
        function index() {
}
        
          

        function  nadd()
          {
         $titulo = $_POST['titulo'];
         $texto = $_POST['noticia'];
         $fecha = $_POST['fecha'];
         /*$cat = $_POST['categoria'];
         */
         $ct=count($titulo);
         $cx=count($texto);
         $cf=count($fecha);
            $this->load->database();

            $data = array(
               'titulo' => $titulo ,
               'noticia' => $texto,
               'fecha' => $fecha,
               /*'cat' => $cat
            */);
          
          if( $this->db->insert('news', $data) == TRUE) {
            $this->load->view('addview');
             }
          else {
          
             echo "No se ha podido realizar la entrada, compruebe los parametros y la configuracion";
              }
              
              
          
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')              
          }
        
        
        
     }
    
?>



View:
Code:
<?
$this->load->helper('url');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head profile="http://gmpg.org/xfn/11"&gt;
&lt;title&gt;Saiend&lt;/title&gt;&lt;link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;meta http-equiv="pragma" content="no-cache" /&gt;
&lt;meta http-equiv="cache-control" content="no-cache" /&gt;
&lt;meta http-equiv="Content-type" content="text/html; charset=UTF-8" /&gt;
>script type="text/javascript" src='&lt;?=base_url()?&gt;js/jquery.js'>>/script>
>script>
function tabular(e,obj) {
  tecla=(document.all) ? e.keyCode : e.which;
  if(tecla!=13) return;
  frm=obj.form;
  for(i=0;i<frm.elements.length;i++)
    if(frm.elements[i]==obj) {
      if (i==frm.elements.length-1) i=-1;
      break }
  frm.elements[i+1].focus();
  return false;
}
>/script>


        
&lt;link rel="stylesheet" href='&lt;?=base_url()?&gt;style/reset.css' type="text/css" media="screen, projection" /&gt;
&lt;link rel="stylesheet" href='&lt;?=base_url()?&gt;tyle/default.css' type="text/css" media="screen, projection" /&gt;
&lt;link rel="stylesheet" href='&lt;?=base_url()?&gt;style/styling.css' type="text/css" media="screen, projection" /&gt;
&lt;link rel="stylesheet" href='&lt;?=base_url()?&gt;style/print.css' type="text/css" media="screen, projection" /&gt;

>script>
        function post() {
            $.post("&lt;?=base_url()?&gt;add/nadd",{  titulo:$("#titulo").val() ,       fecha:$("#fecha").val() ,
   noticia:$("#noticia").val()  },function(response) {
                $("#update").html(response);
            });
        }
        >/script>
    
&lt;/head&gt;


&lt;body class="fullwidth"&gt;

    <div id="container">

        <div id="header">

            <div id="header-in">



            </div> &lt;!-- end #header-in --&gt;

        </div> &lt;!-- end #header --&gt;

        <div id="content-wrap" class="clear">


<div class="main">


<div class="box">
<h1>Sign in</h1>
<p>Do you have an account? Sign in!</p>
&lt;form method="post" action="&lt;?=base_url()?&gt;/add/nadd" id="nvm"&gt;

<label><span>Titulo</span>
&lt;input type="text"name="titulo" id="titulo" class="input-text"&gt;
</label>

<label><span>Fecha</span>
&lt;input type="text"name="fecha" id="fecha" class="input-text"&gt;

<label><span>Noticia:</span>
&lt;textarea rows="10" cols="70" class="fta" name="noticia" id="noticia"&gt;&lt;/textarea>
</label>

<br>



<div class="spacer">&lt;input type="submit" &gt;
</label>
&lt;/form&gt;&lt;/div>
<div class="spacer">
Lost your data? <a href="#">Get your password</a><br/>
New User? <a href="#">Sign up</a>
</div>
</div>

        <div id="update" style="border: 1px dotted red;"></div>


</div>


      </div>
    &lt;!-- end #content-wrap --&gt;
    
    &lt;!-- a�adir otros div, debajo de 1up--&gt;

        <div class="clear"></div>

        &lt;!-- end #footer --&gt;
</div>
    &lt;!-- end div#container --&gt;



&lt;!-- end of content, starting some javascript... --&gt;

&lt;!-- common functions --&gt;


&lt;/body&gt;


</ul>

&lt;/html&gt;

If anyone can help me i think the problem is on the javascript code, but i test with other php code("normal" php code, NO with Framework or something)

Thanks
#2

[eluser]Nick Husher[/eluser]
Question: when is the jquery script ever getting called?
#3

[eluser]finord[/eluser]
Here:
Code:
>script>
        function post() {
            $.post("&lt;?=base_url()?&gt;add/nadd",{  titulo:$("#titulo").val() ,       fecha:$("#fecha").val() ,
   noticia:$("#noticia").val()  },function(response) {
                $("#update").html(response);
            });
        }
        >/script>

thanks
#4

[eluser]Nick Husher[/eluser]
I think you misunderstood, that's where the script is being defined, not where it is being called.

Defining a function in javascript looks like this:
Code:
function MyFunction(args) { ... }
Calling a function in javascript looks like this:
Code:
MyFunction(myArguments);

Note: there are actually a handful of ways to both define and call functions in Javascript, but the most codified ways are delineated above.
#5

[eluser]finord[/eluser]
I use this example form a some forum to do this, and with this code work, i test it.

index.html
Code:
&lt;html&gt;
    &lt;head&gt;
        -script type="text/javascript" src="jquery.js">[removed]
        &lt;title&gt;GET Example&lt;/title&gt;
        -script type="text/javascript">
        function post() {
            $.post("post.php",{  fname:$("#fname").val()  , te2:$("#te2").val() , lname:$("#lname").val()  },function(response) {
                $("#update").html(response);
            });
        }
        -/script>
    &lt;/head&gt;
    &lt;body&gt;
        First Name: &lt;input type="text" name="fname" id="fname" /&gt;&lt;br />
        Last Name: &lt;input type="text" name="lname" id="lname" /&gt;&lt;br />
        Eso &lt;input type="text" name="te2" id="te2"&gt;&lt;br>
        &lt;input type="button"&gt;&lt;br />
        <div id="update" style="border: 1px dotted red;"></div>
    &lt;/body&gt;
&lt;/html&gt;

And the Post.php
Code:
$fname = $_POST['fname'];
    $lname = $_POST['lname'];
    echo "Your full name is $fname $lname. Welcome to the site!";

I try to use jquery docs, but cant do it work, for hat use this example.

Thanks
#6

[eluser]thesf[/eluser]
I think the problem is that your missing the document ready bit within your script tags.

http://docs.jquery.com/How_jQuery_Works#...B.7D.29.3B
#7

[eluser]diego.greyrobot[/eluser]
Yea for one i see some typos in your code
mainly where your script tags are, you have them like this: >script>

make them [removed]

also you need to take $.post(..) out of the post() function
and instead enclose it in ...
$(document).ready(function(){
$.post(...);
});

also you'll probably want to have an event trigger an ajax call like this...
$(document).ready(function(){
$('#submit_button').click(function(){
$.post(...);
});
});

that way the $.post() function at the right time.
#8

[eluser]Colin Williams[/eluser]
What will really help you down the line is to not just copy, paste, and cross your fingers. Learn how the code works.




Theme © iAndrew 2016 - Forum software by © MyBB