1

This is my first week using php, so thanks in advance for your constructive response.

I have a html page that has a url parameter appended: mywebsite.com/f.html?survey_id=5d86055f35bf41f3a35f2c779fc478dc

I need to pass this survey_id parameter to a php script that saves this id in a text file. My current code is not passing correctly as the textfile is empty.

The html page is /f.html The php script is /test.php

This is all being run server-side.

The saving script works, as if instead of $id I just save a string like 'd', everything works properly.

I have tried the below code using $_GET, but the output is empty so I am assuming that the parameter is not being passed to the .php script which is what executes the fwrite.

I have also read that this can potentially be fixed by modifying the .htaccess file, and tried adding the below to my .htaccess file but it did not solve the problem.

RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]

I feel this is not exactly the right rule but do not know how to modify it to fit my particular case.

jQuery(document).on('click', 'div#download', function () {
    jQuery('div#counter1').html('Loading...');
    var ajax = jQuery.ajax({
        method: 'get',
        url: '/test.php', // Link to this page
        data: { 'increase': '1' }
    });
    ajax.done(function (data) {
        jQuery('div#counter1').html(data);
    });
    ajax.fail(function (data) {
        alert('ajax fail : url of ajax request is not reachable');
    });
});

test.php

  $myFile = "testFile2.txt";
  $fh = fopen($myFile, 'w') or die("can't open file");
  $id = $_GET['survey_id'];
  $stringData = $id;
  fwrite($fh, $stringData);
  fclose($fh);

The expected result in the text file should be 5d86055f35bf41f3a35f2c779fc478dc.

The current result is that the text file is empty.

W Hampton
  • 53
  • 7
  • You will have to pass the survey_id to the test.php page as well. – Refilon Feb 13 '19 at 12:25
  • @Refilon Yes, that is what I am asking how to do. – W Hampton Feb 13 '19 at 12:26
  • The only parameter you called test.php with, is `increase` - so you can of course not expect `$_GET['survey_id']` to have any value in there. Your JS code running on the HTML page must read this value, and pass it along … – 04FS Feb 13 '19 at 12:26
  • 1
    So your question boils down to just another duplicate of https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters then …? – 04FS Feb 13 '19 at 12:27
  • Possible duplicate of [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – Refilon Feb 13 '19 at 12:30

1 Answers1

1

Pass the survey_id in data like that

 jQuery(document).on('click', 'div#download', function () {
var url =window.location.search;
    var survey_id = /survey_id =([^&]+)/.exec(url)[1];


        jQuery('div#counter1').html('Loading...');
        var ajax = jQuery.ajax({
            method: 'get',
            url: '/test.php', // Link to this page
            data: { 'increase': '1', 'survey_id': survey_id }
        });
        ajax.done(function (data) {
            jQuery('div#counter1').html(data);
        });
        ajax.fail(function (data) {
            alert('ajax fail : url of ajax request is not reachable');
        });
    });
bhaghyasandar
  • 516
  • 4
  • 16