Jquery with CI - El Forum - 10-03-2008
[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">
<html >
<head profile="http://gmpg.org/xfn/11">
<title>Saiend</title><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
>script type="text/javascript" src='<?=base_url()?>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>
<link rel="stylesheet" href='<?=base_url()?>style/reset.css' type="text/css" media="screen, projection" />
<link rel="stylesheet" href='<?=base_url()?>tyle/default.css' type="text/css" media="screen, projection" />
<link rel="stylesheet" href='<?=base_url()?>style/styling.css' type="text/css" media="screen, projection" />
<link rel="stylesheet" href='<?=base_url()?>style/print.css' type="text/css" media="screen, projection" />
>script>
function post() {
$.post("<?=base_url()?>add/nadd",{ titulo:$("#titulo").val() , fecha:$("#fecha").val() ,
noticia:$("#noticia").val() },function(response) {
$("#update").html(response);
});
}
>/script>
</head>
<body class="fullwidth">
<div id="container">
<div id="header">
<div id="header-in">
</div> <!-- end #header-in -->
</div> <!-- end #header -->
<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>
<form method="post" action="<?=base_url()?>/add/nadd" id="nvm">
<label><span>Titulo</span>
<input type="text"name="titulo" id="titulo" class="input-text">
</label>
<label><span>Fecha</span>
<input type="text"name="fecha" id="fecha" class="input-text">
<label><span>Noticia:</span>
<textarea rows="10" cols="70" class="fta" name="noticia" id="noticia"></textarea>
</label>
<br>
<div class="spacer"><input type="submit" >
</label>
</form></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>
<!-- end #content-wrap -->
<!-- a�adir otros div, debajo de 1up-->
<div class="clear"></div>
<!-- end #footer -->
</div>
<!-- end div#container -->
<!-- end of content, starting some javascript... -->
<!-- common functions -->
</body>
</ul>
</html>
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
Jquery with CI - El Forum - 10-03-2008
[eluser]Nick Husher[/eluser]
Question: when is the jquery script ever getting called?
Jquery with CI - El Forum - 10-03-2008
[eluser]finord[/eluser]
Here:
Code: >script>
function post() {
$.post("<?=base_url()?>add/nadd",{ titulo:$("#titulo").val() , fecha:$("#fecha").val() ,
noticia:$("#noticia").val() },function(response) {
$("#update").html(response);
});
}
>/script>
thanks
Jquery with CI - El Forum - 10-03-2008
[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.
Jquery with CI - El Forum - 10-04-2008
[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: <html>
<head>
-script type="text/javascript" src="jquery.js">[removed]
<title>GET Example</title>
-script type="text/javascript">
function post() {
$.post("post.php",{ fname:$("#fname").val() , te2:$("#te2").val() , lname:$("#lname").val() },function(response) {
$("#update").html(response);
});
}
-/script>
</head>
<body>
First Name: <input type="text" name="fname" id="fname" /><br />
Last Name: <input type="text" name="lname" id="lname" /><br />
Eso <input type="text" name="te2" id="te2"><br>
<input type="button"><br />
<div id="update" style="border: 1px dotted red;"></div>
</body>
</html>
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
Jquery with CI - El Forum - 11-06-2008
[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#.24.28document.29.ready.28function.28.29.7B.7D.29.3B
Jquery with CI - El Forum - 11-07-2008
[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.
Jquery with CI - El Forum - 11-07-2008
[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.
|