1

I wanted a php script to execute when the user press the button. In my html file i used a jquery script

<script>
        $(document).ready(function(){
            $(".btn").click(function(){

                var firstname = $("#firstname").val();
                var lasname = $("#lastname").val();
                var dataNames = { 'firstnameVal' : firstname};
                $.post("SaveNames.php",dataNames,function()
                       {
                    alert("hi");
                });
            });   
        });                
    </script>

and

<input type="text" id="firstname" name="firstname">
<input type="text" id="lastname" name="lastname">

in my php file i used some help from here

<?php

if (isset($_POST["fistname"]))
{
        $firstname = $_POST["firstname"];
        $SavedNames = fopen("Names.txt","w");

        $NamesText = "Its Work!";

        fwrite($SavedNames,$NamesText);
        fclose($SavedNames);
}
?>

The problem is the fopen() doesn't work (don't create file) and I tried to create the file to check if fwrite() works but it's also didn't worked.

I use localhost wamp server.

ben
  • 107
  • 8
jidd6
  • 11
  • 5
  • "don't work" .. your specific description should help us just fine! – Sterling Archer Mar 18 '15 at 15:09
  • In what way does it not work? Do you get an error message (if not, try [turning on error reporting](http://stackoverflow.com/a/845025/992504))? What is the behavior that you're seeing? – Hayden Schiff Mar 18 '15 at 15:11
  • 1
    Check if the folder is writable? – tonoslfx Mar 18 '15 at 15:12
  • the problem is that the file did not created – jidd6 Mar 18 '15 at 15:21
  • While you may have (personally) permissions to write to the directory/file, make sure that the Webserver also has rights. You can do this by setting the group of the directory (and/or file) to the group of the webserver, and then setting group write permissions. – Mike Mar 18 '15 at 16:05

5 Answers5

1

Try to set the complete path to your file:

fopen("c:\\folder\\Names.txt", "w");

Make sure file permissions are set correctly (writeable) to c:\\folder\\. Pay attention to lower-/uppercase naming.

0

Try if this works:

if (isset($_POST["firstname"]))
{
        $firstname = $_POST["firstname"];
        if(file_put_contents("Names.txt", $firstname) !== false)
            echo "It works!\n";
}
jvitasek
  • 810
  • 8
  • 16
0

Firstly, isset($_POST["fistname"]), you mispelled "firstname".

Secondly, on the php side you use $_POST['firstname']. But in the javascript, you have

var dataNames = { 'firstnameVal' : firstname};

Try it with

var dataNames = { 'firstname' : firstname};
Rickkwa
  • 2,197
  • 4
  • 23
  • 34
0

As saided, try checking if the folder in which you are trying to write exist and have privileges to write in. But first clear your mispelling errors and variable names. Code below will post value with name which you declared in brackets. So in your example firstnameVal. So you need to check if this POST value exist.

var dataNames = { 'firstnameVal' : firstname};

If this didn't work use php function var_dump['variableName']; to display data after post method. So you can get handle on data flow in your files.

Nejc Rodošek
  • 143
  • 2
  • 11
0

The problem was from wampserver I reinstall it and it did work.

jidd6
  • 11
  • 5