2

this is returning errors that occur when you try to load an url with [] in it(as far as I know)... I dont know why i am getting this errors... please help me correct the code below:

Errors: Warning: strpos() expects parameter 1 to be string, array given in C:...\query.php on line 1718 <<<
Warning: preg_split() expects parameter 2 to be string, array given in C:...\query.php on line 1719 <<<
Warning: Invalid argument supplied for foreach() in C:...\query.php on line 1720 <<<

$("#submit").live("click", function() {
$("#form").submit(function() {
    event.preventDefault();
    var teste=$(this).serialize();
    $.ajax({
        type: "POST",
        url: "./.../search.php",
        data: teste,
        success:function(data) {
            $("#index_content").html(data);
        }
    });
});
});

Edited version that works :)

$("#submit").live("click", function() {
$("#form").submit(function() {
    event.preventDefault();
    teste = $('#form').serialize();
    $.ajax({
        type: "POST",
        url: "./.../search.php",
        data: { 'album-features': teste },
        success:function(response) {
            $("#index_content").html(response);
        }
    });
});
});
Fábio
  • 91
  • 6

1 Answers1

1

Since you are using wordpress, why not to use its built in ajax and send the request to admin-ajax.php. I see that you are sending it to search.php, which is not a good approach especially for security.

Write your php code to be executed in functions.php and add actions.

This page is containing in its answer the detailed way to do so: Dynamically changing navigation links (next and previous) in Wordpress via AJAX

good luck, I am here to assist you

Community
  • 1
  • 1
Adib Aroui
  • 4,981
  • 5
  • 42
  • 94
  • thanks for answering, but using admin-ajax.php complicates my code too much - something that could be very simple is taken to a whole other level lol. I just discovered how to correct the mistake I did, I'll edit the question with the updated code so future people may get advantage of it. – Fábio Apr 27 '13 at 11:34
  • I am accepting this answer although I do not completely agree as I believe admin-ajax has been made for wp-admin section of the website and is used with hooks. I also can't see what is the problem of posting an array into search.php, as search.php only uses a wp_query. If anyone is interested I can paste my search.php code (the form is made with checkboxes(for taxonomies and categories), then it's sent via ajax and used in search.php to make a wp_query. – Fábio Apr 27 '13 at 11:41
  • btw about security: I'm using wordpress... if I was worried about security I wouldn't use the most used CMS in the world hehe – Fábio Apr 27 '13 at 11:44
  • I was thinking me too that using admin-ajax.php complicates my code, but when used to it, It really made my life easy. I hope you will have time to learn this i am sure you will like. Thank you for your accept and edit – Adib Aroui Apr 27 '13 at 16:34
  • as you know, if you type admin-ajax.php in the browser adress bar, you get nothing. but if you type search.php, maybe you will get something. anyway, It is only an example of security benefit, since me I cannot protect my custom php files, and if I put them in script files, someone can know their location and access them. one of the reasons why I use the backend end ajax in wordpress for front end. – Adib Aroui Apr 27 '13 at 16:37
  • when you have time read about nonces here http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/ – Adib Aroui Apr 27 '13 at 16:42
  • 1
    I edited the code and now I call search.php via functions.php using admin-ajax, as the website you pasted earlier mentioned. Anyways, you can see the work I have done here: http://metalground.net/search (only search for album-features work for now) thanks a lot :-) – Fábio Apr 28 '13 at 18:06
  • nice to hear this. because me too, this approach changed my life :-) – Adib Aroui Apr 28 '13 at 18:08
  • can you tell us what idea behind the grey loading effect in metalground.net/ – Adib Aroui Apr 29 '13 at 15:49
  • lol I didnt log in for a long time, so almost 2 years later i'm replying to your question. anyways, it's a div that I show on ajaxload event and hide on ajaxstop event. – Fábio Mar 23 '15 at 17:16