开发者

404 error ajax, code works with straight php / html

开发者 https://www.devze.com 2023-01-10 21:21 出处:网络
so my code works fine by itself, but I\'m trying to add some ajax into it.I\'m using CI, to do some of the lifting.I don\'t understand why it\'s giving a 404 if the code works fine without ajax.

so my code works fine by itself, but I'm trying to add some ajax into it. I'm using CI, to do some of the lifting. I don't understand why it's giving a 404 if the code works fine without ajax.

Here is the form:

 <div class="divider" id="contact">
    <p class = "header"><a id="contactheader" name="gocontact">Contact</a></p>
    <div id = "contactform">
     <form method = "post" id="contactform" action="<?php site_url()?>index.php/home/sendemail">
      <div id ="formtitles">
          <p class = "info">You:</p>
          <p class = "info">Me:</p>
          <p class = "info">Subject:</p>
          <p class = "info">Body:</p>
          <input id = "submit" type="submit" value="Send" />
      </div>
      <div id ="formfields">
       <input id="you" type="text" name="you" /><br/>
       <p class = "info">@gmail.com</p>
       <input id ="subject" type="text" name="subject" /><br/>
       <textarea id = "contactbody"></textarea>
      </div>
     </form>
</div>
</div>

The .js

    $(document).ready(function() {

 $('#submit').click(function(){

  var contactformdata = {
   you: $('#you').val(),
   subject: $('#subject').val(),
   message: $('#message').val(),
   }

  console.log(you);

  $.ajax({
   url: "trenthauck.com/index.php/home/sendemail",
   type: 'POST',
   data: contactformdata,
   success: function(){
     $('#contactheader').replaceWith("<p class='header'>Thanks</p>");
     $('#contactform').remove();
     $('#contactlink').remove();
     $(document).scrollTop(25);
   }
  });

  return false;
 });
       });

And finally the controller:

<!--
Name: Trent Hauck
Date: INSERT开发者_JAVA百科
File: INSERT
Desc: INSERT
-->

<?php



 class Home extends Controller{

  function index(){

   $this->load->view('home_view');
   return true;
  }

  function sendemail(){
   $to = "trent.hauck@gmail.com";
   $from = $this->input->post('you');
   $subject = $this->input->post('subject');
   $message = $this->input->post('contactbody');
   $message = wordwrap($message, 75);

   $tosend = "From: " . $from . "\nMessage: " . $message;

   mail($to, $subject, $tosend);

   $this->index();

  }


 }

Side question, assuming you're still reading. Is there anyway to do something like site_url() from CI in the .js so I don't have to call the whole thing. Thanks


You forgot http:// at the beginning of your Ajax url parameter:

So the browser thinks you're pointing it to [site_url]/trenthauck.com/index.php/home/sendemail, which, in all likelihood, isn't what you intended.

0

精彩评论

暂无评论...
验证码 换一张
取 消