0

I have a page called post-blog.php, in here I've set-up a blog entry. I have all this data being saved into one variable. Its displaying this data in an array.

var data = title + content + image + datetime + categories;

How can I send this data to another page called publish.php and redirect the user to that page ? I've tried to set up a ajax to do this but its not working. Any suggestions ?

  $.ajax({
        type:   'POST',
        cache:    false,
        url:      'publish.php',
        data:     data,
        success:  function( data ) {

          alert ( data );

        }

      });

      return false;

    });
Ryan Holmes
  • 547
  • 6
  • 14
  • Could you add a bit more details on what you want as a result? – Thaillie Nov 21 '15 at 13:34
  • Right I have a variable called data which is holding information, when the user clicks on the button I want the var data to be (Temporally saved) and sent to publish.php , but I also want it to send the user to that page automatically. On that page I just want to alert(data) and show the information in var data – Ryan Holmes Nov 21 '15 at 13:38
  • Is this helpful ? Thanks for your response – Ryan Holmes Nov 21 '15 at 13:39
  • Yes its helpful, I would suggest [this](http://php.net/manual/en/reserved.variables.get.php#98419). You can give the parameters through the URL to the next page. – Thaillie Nov 21 '15 at 13:42
  • 1
    Why are you using Ajax if you have to redirect to another page? Just use simple post!! – Amit Singh Nov 21 '15 at 13:43
  • I dont have to use Ajax Im new to this so I thought Ajax would be more useful? – Ryan Holmes Nov 21 '15 at 13:45
  • Ryan ajax is mostly used only if you want the data to be posted to some page without redirection. I don't think you need to use ajax here. As long as your purpose is to learn. You are very welcome. – Amit Singh Nov 21 '15 at 13:46
  • @RyanHolmes Yea, you can just use `$_POST` or `$_GET` for this. Ajax isn't necessary. – Thaillie Nov 21 '15 at 13:48
  • Thanks so you would recommenced sending the data value to another page via php? – Ryan Holmes Nov 21 '15 at 13:49
  • You also need to wrap your ajax function inside $(document).ready(function(){}); – Amit Singh Nov 21 '15 at 13:49
  • @RyanHolmes I guess you wanted to do something like this http://www.w3schools.com/php/php_forms.asp – Thaillie Nov 21 '15 at 13:51

2 Answers2

0

As per my understanding of the problem, you need to pass the data to a new page and open that page.

If this is your question then this can be done without AJAX, basically AJAX does not even provide solution here. Instead you can just pass all the data to your new page in query format as below -

var page = 'publish.php?title='+title+ '&content='+content+'&image='+image+ '&datetime='+datetime+'&categories='+categories;

Then just change the window location as below

window.location.href = page;

And to get all those variables in your PHP file, do the following in publish.php on top -

if($_GET['title'])
{
  $title = $_GET['title'];
}
// similarly get all the data in publish.php file and continue with your page
Chandan
  • 1,128
  • 9
  • 11
  • This is exactly what i'm looking for, sorry i'm new and didnt reaslise you needed to ajax. – Ryan Holmes Nov 21 '15 at 14:11
  • I'm getting a error - Uncaught SyntaxError: Unexpected identifier By the var page ? – Ryan Holmes Nov 21 '15 at 14:11
  • can you please paste your `data` content here.. and full error ? – Chandan Nov 21 '15 at 14:28
  • Right I'm not getting errors anymore mistake on my part. Its not redirecting to page though var page = 'publish.php?title='+title+ '&content='+content+'&image='+image+ '&datetime='+datetime; window.location.href = page; – Ryan Holmes Nov 21 '15 at 14:39
  • Can you please post the value of `page` variable ? and where you are putting this in your code ? – Chandan Nov 21 '15 at 14:43
  • Thanks for your help. I've added all the jquery on the button click here - https://jsfiddle.net/ueLft8x2/ . The code wont run because its using other functions on the page. Is this useful? – Ryan Holmes Nov 21 '15 at 14:46
  • I've uploaded all the code in that section here. The functions wont run because that gathering the data which is done elsewhere [link](https://jsfiddle.net/ueLft8x2/) Is this helpful? – Ryan Holmes Nov 21 '15 at 14:48
  • `var page = 'publish.php?title='+title+ '&content='+content+'&image='+image+ '&datetime='+datetime+'&categories='+categories;` + was missed before categories. – Chandan Nov 21 '15 at 14:55
  • does this work for you? I tried and it worked for me. corrected in answer as well. – Chandan Nov 21 '15 at 14:56
  • The var is storing the correct information, but not redirecting ? – Ryan Holmes Nov 21 '15 at 15:36
  • paste here the redirection code that you are using ? – Chandan Nov 21 '15 at 15:48
  • window.location.href = page; – Ryan Holmes Nov 21 '15 at 15:50
  • use this one `window.location = page;`. One thing you also want to try is putting the absolute url. ie `var page = 'http://....'` like this.. – Chandan Nov 21 '15 at 15:53
-1

I am assuming all your variables are strings. If they are not, for example the datetime may be an object, change them into a string first.

Docs say Object must be Key/Value pairs or a string.

Objects work well for this, try something like:

var data = {title: title, content: content, image: image, datetime: datetime, categories: categories};

If your data is coming from a form check out jQuery's serialize.

I've never tried to pass as a string in a POST, but my gut feeling is it would need to be in a format similar to passing the data through the url.

var data = 'title=' + title + '&content=' + content;

Also keep in mind the data in the success function is not the same as what is being passed to the php page. This is what the php page will return. If you're php page returns nothing your alert will be empty. When I'm testing I like to throw something like echo $_POST['title']; in the php file to see something come back.

Here is a similar question that might help too.

Community
  • 1
  • 1
Deplicator
  • 34
  • 5
  • Updated the response; I know the docs say it accepts a string but I've always had better luck as an object. – Deplicator Nov 21 '15 at 13:41