Welcome Guest, Not a member yet? Register   Sign In
Where to put my PHP script?
#1

[eluser]solid9[/eluser]
Hi guys

I have these jquery codes below,
Code:
$(document).ready(function(){
/* The following code is executed once the DOM is loaded */

/* This flag will prevent multiple comment submits: */
var working = false;

/* Listening for the submit event of the form: */
$('#addCommentForm').submit(function(e){

   e.preventDefault();
  if(working) return false;
  
  working = true;
  $('#submit').val('Working..');
  $('span.error').remove();
  
  /* Sending the form fileds to submit.php: */
  $.post('submit.php',$(this).serialize(),function(msg){

   working = false;
   $('#submit').val('Submit');
  
   if(msg.status){

    /*
    / If the insert was successful, add the comment
    / below the last one on the page with a slideDown effect
    /*/

    $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
    $('#body').val('');
   }
   else {

    /*
    / If there were errors, loop through the
    / msg.errors object and display them on the page
    /*/
    
    $.each(msg.errors,function(k,v){
     $('label[for='+k+']').append('<span class="error">'+v+'</span>');
    });
   }
  },'json');

});

});

As you can see it call the submit.php

Now my question is since this jQuery script is called from my VIEW page.
Where do I put my submit.php script?
In the controller?
In the model?
Or In the view as well?

Hope someone will give shed.

Thanks in advanced.
#2

[eluser]solid9[/eluser]
I tried putting the submit.php into the /view/ directory
But it doesn't work.

Here is the codes for the submit.php
Code:
&lt;?php

// Error reporting:
error_reporting(E_ALL^E_NOTICE);

// This array is going to be populated with either
// the data that was sent to the script, or the
// error messages.

//echo '<pre>';
//print_r($arr);
//echo '</pre>';

$arr = array();
//$validates = Comment::validate($arr);
$validates = $this->Comment->validate($arr);

if($validates)
{
/* Everything is OK, insert to database: */
mysql_query(" INSERT INTO comments(swapid,name,url,email,body)
     VALUES (
      '".$arr['swapid']."',
      '".$arr['name']."',
      '".$arr['url']."',
      '".$arr['email']."',
      '".$arr['body']."'
     )");

$arr['dt'] = date('r',time());
$arr['id'] = mysql_insert_id();

/*
/ The data in $arr is escaped for the mysql query,
/ but we need the unescaped variables, so we apply,
/ stripslashes to all the elements in the array:
/*/

$arr = array_map('stripslashes',$arr);

$insertedComment = new Comment($arr);

/* Outputting the markup of the just-inserted comment: */

echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));

}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}

?&gt;

#3

[eluser]astroanu[/eluser]
in codeigniter you cant call a php file directly. you must make a controller function and call it.

so your ajax call should be something like

Code:
$.post('ajax_controller/submit',$(this).serialize(),function(msg){

also in the submit.php:
put the database call in to a model and call it from the controller.




Theme © iAndrew 2016 - Forum software by © MyBB