0

So I see a lot of information regarding PHP post/get methods for passing data around, but I have an interesting issue that I would like some help resolving.

I have an HTML page that has some data on it in various inputs, and I also run some algorithms on the JavaScript backend. I would like to pass ALL of this data to a PHP page - the primary problem here is that my data is split between HTML and JavaScript, so the obvious choice is to read in the HTML values to the script using basic DOM. However, this means that I have to POST data via the JavaScript which seems like the job of AJAX. Here's my code -

var userData = {

    data: "data"ss

}

jsonData = JSON.stringify(userData);
alert(jsonData);

$.ajax({  
    type: 'POST',  
    url: 'mypage.php', 
    data: userData,
    datatype: json,
    success: function() {
        window.open("mypage.php");
    },
    error: function(err, text){
        alert(text);
    }
});

window.open("mypage.php");

I'm just getting familiar with PHP, so I really want to know how I can get the data I post using the AJAX to show up in the final PHP page because right now it is not doing that. All my inputs are remaining blank. I can provide more details - I am using a web hosting service that is PHP enabled, so I know that's not the problem.

I'm also aware that I could plug my JavaScript values into HTML inputs and just use the form, but that does not seem very secure. Anyway, help is appreciated!

4
  • 2
    Sidenote: Is that "ss" in there accidentally? data: "data"ss <= I doubt that should be in there. Commented Feb 17, 2015 at 4:54
  • If you want to navigate to the page you should use a form. There are no security benefits in using ajax over a form. Commented Feb 17, 2015 at 4:59
  • Terminology nit-pick: Unless you're running something like Node.js on your server, then your JavaScript is almost certainly part of your front-end, running in the browser. PHP would be your back-end (running on the server). I bring this up because mixing up or not understanding the separation of front-end vs. back-end, and code running on client vs. server, is an extremely common mistake made by those learning web-based programming. Commented Feb 17, 2015 at 5:04
  • So there's no way to open PHP pages like a form through JavaScript? I'm trying to avoid using hidden inputs because I don't want users to be able to View Source and see the JavaScript values that are being passed through. I know PHP is a server side script; I'm just using it within a larger HTML document to populate certain areas with data inputted directly by the user (via inputs) and data derived through my algorithm (the JavaScript). If I do put the JavaScript values in hidden, will they be visible to a View Source action? Commented Feb 17, 2015 at 5:42

1 Answer 1

0

Actually, Ajax is not what you are looking for. The purpose of an Ajax request is to load/send data "silently", without changing the document location.

You may have to create a form (using jQuery), set its "action" attribute to your page URL, add your data in it (using hidden fields), then submit it.

This way, your data will be passed to the new page, and you'll be able to use $_POST in PHP to retrieve them, and feed the values of your form with them.

Sign up to request clarification or add additional context in comments.

1 Comment

This was where I started, I was just trying to do everything in the script to prevent View Source access to the algorithm outputs. It's not that big of a deal, but I personally prefer more elegant and dynamic programming - the hidden input solution seems a bit brute force. But if it works, I guess I should go with it lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.