How to save text field data in a text file with multiple values separated by comma?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sufian
    New Member
    • Jul 2006
    • 9

    How to save text field data in a text file with multiple values separated by comma?

    Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message:

    [PHP]echo("<div id=\"message\" class=\"success \">Thank you! You have been successfully registered.</div>");[/PHP]
    within 1.5 seconds.

    [PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Testin g Homepage</title>
    <link href="main.css" rel="stylesheet " type="text/css" media="screen" />
    </head>
    <body>
    <script type="text/javascript" language="javas cript">
    // Set path to PHP script
    var phpscript = 'subscribe.php' ;
    function createRequestOb ject() {
    var req;
    if(window.XMLHt tpRequest){
    // Firefox, Safari, Opera...
    req = new XMLHttpRequest( );
    req.overrideMim eType('text/xml');
    } else if(window.Activ eXObject) {
    // Internet Explorer 5+
    req = new ActiveXObject(" Microsoft.XMLHT TP");
    } else {
    // There is an error creating the object,
    // just as an old browser is being used.
    alert('There was a problem creating the XMLHttpRequest object');
    }
    return req;
    }

    // Make the XMLHttpRequest object
    var http = createRequestOb ject();
    function sendRequestPost (data) {
    var emailPat = "^([0-9a-z]+)([0-9a-z._-]+)@([0-9a-z._-]+)\.([0-9a-z]+)";
    var vemailid=docume nt.getElementBy Id('email1').va lue;
    var matchArray = vemailid.match( emailPat);
    if(document.get ElementById('em ail1').value == '')
    {
    alert("'Email Address' field can not be blank.");
    document.getEle mentById('email 1').focus();
    }
    else if (matchArray == null)
    {
    alert("Your email address seems incorrect. Please try again.");
    document.getEle mentById('email 1').focus();
    }
    else {
    http.open('post ', phpscript, true);
    http.setRequest Header('Content-Type', 'application/x-www-form-urlencoded');
    http.setRequest Header("Connect ion", "close");
    http.onreadysta techange = handleResponseP ost;
    http.send('emai l1='+data);
    }
    }
    function handleResponseP ost() {
    if(http.readySt ate == 4 && http.status == 200){
    var response = http.responseTe xt;
    if(response) {
    document.getEle mentById("dt"). innerHTML = response;
    }
    }
    }
    </script>
    <div class="subscrib e">
    <br />
    Email Address: <input type="text" name="email1" id="email1" maxlength="36" size="36" />
    <input type="button" name="go" value="Register " onclick="sendRe questPost(docum ent.getElementB yId('email1').v alue);" />
    <div id="dt"></div>
    </div>
    </body>
    [/PHP]</html>


    This is subscribe.php:
    [PHP]<?php
    if(isset($_POST['email1'])){
    $email = $_POST['email1'];
    echo"<br>";
    echo("<div id=\"message\" class=\"success \">Thank you! You have been successfully registered.</div>");
    }
    ?>[/PHP]

    Now my question is:
    How to save text field data (i.e. the email address entered by the user) in a text file with multiple values separated by comma? Or in other case how to save these values in an Excel CSV file. We do not want these values to be stored in a database but just want a text file to be created on the first entered value and the rest of the values when entered second time or third time should be appended with the separation of comma i.e:
    abc@abc.com,bbb @bbb.org.uk, ccccccccddd@efg hi.com
    and like that.
    Can any one help?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    So you want this file on the server?

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      Why on earth would you *not* want this to be in a database?

      And BTW, you can do the file alteration very easily through PHP with fopen(), fread(), and fwrite().

      Comment

      • sufian
        New Member
        • Jul 2006
        • 9

        #4
        Hi guys
        Thx for replying. Sunday was officially off, therefore I couldn't reply.
        Let me tell you the whole story:
        Actually we are a software company, and nowadays the workload is huge, and during this huge workload there comes a client who want their website to be developed in 15 days! After negotiation client understood that this is not posible, therefore by mutual undertanding we (client and us) have decided to just build an under construction website (that is a web site with only a single page). But the client insisted that there should be an email field for the visitor to enter his email address and because we are in a hurry we are not building a database, just a text file to be on the server (as 'acoder' i.e. the 'moderator' said). After 1.5 months or so we will start developing their database and the rest of the website and therefore will want those email addresses to be kept in a column and these email addresses will also be used to send an official email to the registered visitors that now the website is no more under construction etc.
        Since I am new to the world of PHP, therefore I want to ask about fopen() and fwrite(). Where this function should be written in case of Ajax POST request as already mentioned in by previous message? How will this take the data inserted in text field to the text file with a comma between these addresses?

        Comment

        • sufian
          New Member
          • Jul 2006
          • 9

          #5
          Hey finally resolved. Thx a lot both of you.

          Comment

          Working...