0

I am certainly new to Web Development. I have read tonnes of articles and answers on this issue but haven't found the solution yet. I want to store some user-generated data into a database.

I know the PHP scripts are loaded once when the page is loaded. I want to send some data that I collect from the user after the page is loaded, and store it in javascript and then send the data to PHP. I understand that either the data can be sent via form by defining the action attribute or using requests. But I don't exactly understand how to send and receive the data through requests.

I tried sending the data to the page itself

$.ajax({
            type:"POST",
            cache:false,
            url:"localhost/quiz/index.php",
            data:{
                name : userName,
                email : userEmail,
                website : siteURL,
                noLeads : leads,
                content : regularContent,
                seo : seoBefore,
                paidAds : otherPaidAds,
                socialMedia : socialMediaAds,
                emarketingExpenditure : expenditureMarketing,
                agency : marketingAgency,
                growth : businessGrowth
            },
            success: function () {
                alert("Data sent "+"<?php echo $_POST['name']; ?>");
                window.location.href = "dataController.php";
            }
            });

But then, the PHP scripts are loaded only once, when the page loads. So when I try to fire an alert it either says that the POST variable is undefined or it takes the value which I set at the beginning of the page during variable declaration.

If I send the request to another file:

$.ajax({
            type:"POST",
            cache:false,
            url:"localhost/quiz/index.php",
            data:{
                name : userName,
                email : userEmail,
                website : siteURL,
                noLeads : leads,
                content : regularContent,
                seo : seoBefore,
                paidAds : otherPaidAds,
                socialMedia : socialMediaAds,
                emarketingExpenditure : expenditureMarketing,
                agency : marketingAgency,
                growth : businessGrowth
            },
            success: function () {
                alert("Data sent "+"<?php echo $_POST['name']; ?>");
                window.location.href = "dataController.php";
            }
            });

But then I don't know how to use that data. I took the input and redirected the tab to the second page, and on the second page I tried to print/alert the POST['name'] variables that were sent from the first page, but it says the variables are undefined.

So how does the sending and receiving of data on servers between different or the same file works? How and when that data can be used?

  • I'd suggest experimenting with ajax in a smaller test script. To learn the ropes first. Then test it out in your project with those insights. – GetSet Feb 03 '20 at 11:35

1 Answers1

0

You will send data from js to php in order to write into a database or to use the data to compare / (query) select from database.

Other than these two, you shall straight away use those js variables inside your html.

Secondly the php function

$_POST['name'] 

will be available in your localhost/quiz/index.php and NOT in the same file in which the calling js resides !

Please refresh (by going through study materials) yourself regarding server side and client side.

The client side code sends data to the server side to do your preferred operation. You can not have php to do things at client side (except content delivery).

Also, no need for you to try sending data from js to php, and to send php data to the js through some method or other.

  • Yes, I want to store all that data into a database, I am using a javascript library for and the data is stored in the variables I sent in ajax request. But how do I store that data into a database if I don't even have it in PHP? – Samarpit Shrivastava Feb 03 '20 at 11:47