www.webdeveloper.com
Results 1 to 5 of 5

Thread: Form to file problem

  1. #1
    Join Date
    Mar 2014
    Posts
    3

    Form to file problem

    Hi,
    I'm trying for some time to get this script to work.
    It's a script that can be found all over the Internet.
    Whatever I tried, I can't get it to work.
    Maybe a simple typo but I cant find it.

    It's a 1 field form, posting a text to a file.

    The content is not saved to the file, the file ends up empty every time, I get no response in the <status div.

    If someone could point me to the error I would be more than grateful.

    Thanks
    JohN

    process-form.php
    Code:
    <?php 
    
    $myFile = "/home/adm/domains/mydomain.nl/public_html/frm01/clnt01.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $_POST['sc']);
    fclose($fh);
    echo 'file is ' . $_POST['sc']; // just for testing purpose returning the content
    
    ?>
    html page
    Code:
       <head >
    
    <script type="text/javascript">   
    function ajax_post(){
        // Create our XMLHttpRequest object
               var hr = new XMLHttpRequest();
    		   
        // Create some variables we need to send to our PHP file
               var url = "process-form.php";
    	   var sc = document.getElementById("kontent") . value;
               hr.open("POST", url, true);
    		   
    		   
        // Set content type header information for sending url encoded variables in the request
               hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		   // hr.setRequestHeader("Content-length", postdata.length);
    		   
        // Access the onreadystatechange event for the XMLHttpRequest object
               hr.onreadystatechange = function() {
               if(hr.readyState == 4 && hr.status == 200) {
               var return_data = hr.responseText;
               document.getElementById("status").innerHTML = return_data;
       }
        }
    	
        // Send the data to PHP now... and wait for response to update the status div
    	// Actually execute the request
    	       hr.send(sc); 
               document.getElementById("status").innerHTML = "processing...";
    }
    
    </script>   
       </head>
       <body>
    <form method="post" action="process-form.php">
    
              <textarea cols="80" id="kontent" name="kontent" rows="10"><p>initiele tekst ...</p></textarea>
    		  
    		  <input type="button" value="Submit" onclick="javascript:ajax_post();">
    		  
               </form>
    		   
    	        <div id="status"></div>
    
       </body>
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    868
    You aren't posting the data to PHP correctly. When using AJAX, the string you post to PHP must be a name/value pair set following standard URL argument format (eg. var_name=some_value&another_var=test).

    In your case you need to change the line where you set the variable named sc.
    Code:
    var sc = "sc="+document.getElementById("kontent").value;
    Also in PHP you should always check variables so that you aren't trying to read/pass undefined/empty variables into files or databases. An example would be
    PHP Code:
    <?php
      $sc 
    = (isset($_POST['sc'])) ? $_POST['sc'] : "";
      
    $myFile "/home/adm/domains/mydomain.nl/public_html/frm01/clnt01.txt";
      
    $fh fopen($myFile'w') or die("can't open file");
      
    fwrite($fh$sc);
      
    fclose($fh);
      echo 
    'file is ' $sc// just for testing purpose returning the content
    ?>
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Mar 2014
    Posts
    3
    Sup3rkirby, thanks a lot for your input.
    Unfortunately it doesn't solve the problem.
    I've made the changes you suggested but the content is still not saved to the file.

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    868
    Could you post your updated source code? I'd like to see how you implemented my changes into your code.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  5. #5
    Join Date
    Mar 2014
    Posts
    3
    Quote Originally Posted by Sup3rkirby View Post
    Could you post your updated source code? I'd like to see how you implemented my changes into your code.
    Got it to work!
    The problem was not my code with your changes.
    The problem was caused by CKEdit. As soon as I left that out, it worked great.
    Switching (take over) of the inputfield by CKEditor, it refused to save the content.

    Wil try to get that to work but this problem is solved with your help.
    Thanks a lot Sup3rkirby

    JohN

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles