Jump to content

Ajax method give me back allows an 404 error


Marco Ro
 Share

Recommended Posts

Hi guys,

I'm not sure if this is a pw issue, anyway I would like to check. 

I have ajax call for a contact form that give me back every time an 404 error. I tried differente solution but doesn't work.

I tried: 

  • <?php echo $config->urls->templates?>
  • <?= $pages->get('/')->url ?>

In both cases the url isn't wrong, but in console keep show me the 404 error. 

This is my ajax code

var datiform = $("#Form").serialize();
$.ajax({
    type: "POST",
    url: "<?php echo $config->urls->templates?>form/engine.php",
	data: datiform,
    crossDomain: true,
	dataType: "html",
        success: function(){
            alert("success" + datiform);
         },
         error: function(){
            alert("no success " + datiform);
        }
	});

 

I can't understand why <?php echo $config->urls->templates?> not works, if it works well example for the pictures. 

 

Link to comment
Share on other sites

There is a big world around PW! every time discover something new it leaves me speechless! But yes, don't have possibility to directly access php files in the "templates"  directory and other locations in PW's file system it's really a great things! Good to know, I was going crazy o.o 

Thank you (again) @Zeka fro your support!

  • Like 1
Link to comment
Share on other sites

Sorry @Zeka there is the possibility the pw block or do something else to the data ? if I try to use parse_str  to decode them not works? 

I mean I have this ajax:

var data = $("#FormWksp").serialize();

  //chiamata ajax
    $.ajax({

    	type: "POST",
    	url: "form-wksp-engine.php",
		  data: data,
		  dataType: "html",
	    success: function()
	    	{
          alert("success" + data );
	     	},
	    error: function()
	    	{
          alert("no success " + data);
	    	}
	    });

And then I try to take it in this way in the engine.php file

  $email = urldecode($_POST['email']);  // this works 
  $phone = urldecode($_POST['phone']); // this works 
  $company = urldecode($_POST['company']); // this works 
  $nation = urldecode($_POST['nation']); // this works 
  $messaggio = urldecode($_POST['messaggio']); // this works 


  $_POST_data = urldecode($_POST["data"]); // this no works 
  $articles = [];
  parse_str($_POST_data, $articles);

  $nome = $articles['nome']; // this no works 

I will use the $articles for make a foreach($articles as $name => $value).

But suppose the $_POST_data = urldecode($_POST["data"]) or $_POST_data = $_POST["data"] get the data, but is not so. Could be the pw in some way do something? 

Because now I move outside the root the engine.php for test it, and works, but this data not pass. 

Link to comment
Share on other sites

@MarcoPLY

You still posting to PHP file.  Instead, you can create a template, for example 'ajax'. Create a template file for this template. Now you have endpoint URL for your requests. 

site.com/ajax/

Then in your template file, you can do something like

if($config->ajax) {
	if($input->post->email) {
		do something
	}
} else {
	// $session->redirect("/"); 
	// or
	//throw new Wire404Exception();
}

Furthermore, you can enable URL segments for 'ajax' template and then you will be able to make requests to URLs like 'ajax/form/', 'ajax/cta/' etc.

if($input->urlSegment2)
throw new Wire404Exception();
if(!$config->ajax) 
throw new Wire404Exception();

if($input->urlSegment1 && $input->urlSegment1 == 'form') {
	do something
}

Hope you got the main idea. 

  • Like 1
Link to comment
Share on other sites

ok, but I have move outside pw the engine.php, now is in the same folder of site and wire folders.

If I use the normal $email = urldecode($_POST['email']);   I can take data to send the e-mail.

so, I need anyway to make a template inside the site/template and create a template e page in the backend?

I found the problem and not was about pw. I don't need use parse_str because the date are already in array. So $POST = $_POST; it's enough.

Edited by MarcoPLY
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...