2

I want to write a txt file which format from latitude, longitude become to latitude longitude

how can I achieve it?

html:

<form method = "post" name = "searchbar">
        <input type="text" name="search" id="SearchBar" placeholder="input a ip">
        <br>
        <button type="button" onclick="getLocation()">get ip</button>
        <button type="submit" name = "writeip" id="id_Writeip">submit</button>
    </form>

php:

    if(array_key_exists('writeip', $_POST)) {
        writeip();}

    function writeip(){
        $myfile = fopen("testing.txt", "w") or die("Unable to open file!");
        $txt = $_POST["search"];
        fwrite($myfile, $txt);
        fclose($myfile);}

script:

<script>
        function getLocation()
        {
                navigator.geolocation.getCurrentPosition(showPosition);
        }
        
        function showPosition(position)
        {   
            document.getElementById("SearchBar").value = position.coords.latitude + ", " + position.coords.longitude;
        }
</script>
Quenton
  • 23
  • 4
  • The submitter element of the form is not passed to the back-end when posting a form. You need to check `search` instead of `writeip` in PHP. – Teemu Apr 30 '21 at 08:31
  • @Teemu Thanks for your response, may I know what can I do to pass data to back-end? Actually I am using local server that is just a file in my pc to save my data.. and I am trying to transfer data in there. – Quenton Apr 30 '21 at 08:42
  • seems something wrong in my post. I want the format from latitude, longitude become to latitude \n longitude – Quenton Apr 30 '21 at 08:45
  • "_just a file in my pc_" is not a local server, you've to install a real local server which can run PHP. Search for XAMP/WAMP for details. – Teemu Apr 30 '21 at 08:45
  • Yes I am using XAMPP and the generated txt file will save inside the XAMPP htdocs folder. – Quenton Apr 30 '21 at 08:49
  • Well, then it's not "_just a file in my pc_". If you want to include a line-break in your data, you've to use a textarea element instead of input, input can't contain line-breaks, and if they're included in the value, they're stripped out from the value. – Teemu Apr 30 '21 at 09:03

1 Answers1

0

If I understand correctly, you need to see if the value is posted as part of the form submitted. In that case it can be checked like this.

if(isset($_POST['search'])) {

    // You also noted you want the cords on new lines.
    $search = $_POST["search"];
    $data = explode(", ", $search);
    $cords = implode("\n", $data);

    writeSearch($cords);
}

function writeSearch($data) {

    $myfile = fopen("testing.txt", "w") or die("Unable to open file!");        
    fwrite($myfile, $data);
    fclose($myfile);
}

In this case we check if the post contains the search value, and then call the writeSearch function. The value we checked is then written to the file.

Daniel ZA
  • 306
  • 2
  • 10
  • 1
    I think yes. What I want to achieve is to "line feed" when meet the ", " in the writing. – Quenton Apr 30 '21 at 09:00
  • I updated the example to show how this can be done. It will split it over 2x lines, and then write out the result for you. Hope it helps :-) – Daniel ZA Apr 30 '21 at 09:02