SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I convert a variable into a string?

    Hi All,

    I am designing a feedback page for my website and have carried info through the url to the feedback page from a previous page. This url contains a name of an individual, in between two symbols = and &

    The url of the feedback page looks something like this:

    www.website.com/feedback.html?name=David_Smith&

    First, I have used a form to display the information so that the user can see who they have selected, ie. extract the first and last names from the url. (The script following the form extracts the name from the url so that it can be displayed by the form, not exactly sure why it does so in this particular order but it seems to work so far).

    This is the code I have used to do this:

    <FORM NAME="SWnamefunc">
    <div align="left">
    <INPUT TYPE="hidden" NAME="yoyo" SIZE="35">
    </div>
    </FORM>

    <div align="left">
    <p>
    <SCRIPT LANGUAGE="javascript">

    var locate = window.location
    document.SWnamefunc.yoyo.value = locate

    var text = document.SWnamefunc.yoyo.value

    function delineate(str){

    theleft = str.indexOf("=") + 1;
    theright = str.lastIndexOf("&");
    return(str.substring(theleft, theright));

    }
    document.write("You have requested a class with: " +delineate(text));
    </SCRIPT>


    I have two questions:

    1. I want to now use the name I have extracted, displayed through the variable text above, and create a string (or label as described below) from that variable - so that it can be then sent to a php script. The php script then emails to me all the information inputted by the user/surfer.

    How do I do this?



    2. What I have done is display the names separated by an _ . Can anyone tell me how I separate the names out and display them without the underscore (in the form above)?

    I am quite a basic coder and have very little knowledge of programming language so any help would be greatly appreciated. If you could write the actual code required - that would be great (ie. instead of saying you need to "do this", please type the code).



    P.S.

    The following code is what I have later used to send the information to a script.

    <form action="feedback.php" method="post">
    <table border="0" cellpadding="8" cellspacing="8" summary="feedback form">
    <tr><td width="115"><label for="tswname"><span class="Bold">*</span>Name</label>/td><td width="361"><input type="text" name="fullname" id="tswname" size="25" /></td></tr>

    I think I need to maybe create a label for the name text (and then direct the form to send the label info to the script). Is this right? And if so - how do I code this?

    Any help would be much appreciated. Cheers!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    1. I want to now use the name I have extracted, displayed through the variable text above, and create a string (or label as described below) from that variable - so that it can be then sent to a php script. The php script then emails to me all the information inputted by the user/surfer.
    The .toString() method is what you will need but i don't understand why you would need to convert the variable to a string when plain text is already a string unless it contains numeric values.

    Example
    Code JavaScript:
    var myNumber = 1400;
    myNumber = myNumber.toString();
    alert(typeof myNumber);

    Outputs "string"

    2. What I have done is display the names separated by an _ . Can anyone tell me how I separate the names out and display them without the underscore (in the form above)?
    Depending on how you want to output it depends on the value you need to use in the .split() method.

    Example
    Code JavaScript:
    var myString = 'Hello in there';
    myString = myString.split(' ');
    alert(myString[0] + ' ' + myString[2]);

    Outputs "Hello there"

  3. #3
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I have modified your script somewhat, but the end result is a christian name and a surname shown in an alert(). You can use the source of these values in your page. I have also changed your "hidden" input to a "text" input so that you can see the whole url that has been passed between the pages. You can change this back to "hidden" to meet your needs.

    To allow me to duplicate passing the required information between pages in the url, I have included below two HTML pages. To make it all work both pages need to be in the same folder and you need to start by opening page1.htm. On loading it will open page2.htm and pass the required info.

    If you look to the right of page2.htm you will find a representation of the string for each part of the splitting process. This may help you see how it is all working.

    Code HTML:
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
     
    <head>
     
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Page 1 Load First</title>
    </head>
     
    <body onload="location.href='page2.htm?cName=John&amp;sName=Smith'">
     
    <!-- pass info as search string appended to url to next page -->
     
    </body>
     
    </html>

    This is the second page. It will be automatically loaded by the first page.

    Code :
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
     
    <head>
     
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Page 2 Loads Automatically</title>
    <script type="text/javascript">
    <!--
    function init()
     { var locate=window.location.href;
      // write full url to page 
       document.SWnamefunc.yoyo.value=locate;
      // get the search string without the ?
       var str=location.search.substr(1);           // looks like this    "cName=John&sName=Smith"
     
      // split at the & character and place in array allParts 
       var allParts=str.split("&");                 // allParts[0]="cName=John, allParts[1]="sName=Smith"
     
      // split first part at the = character and place in array firstPart 
       var firstPart=allParts[0].split("=");        // firstPart[0]="cName", firstPart[1]="John" 
     
      // split second part at the = character and place in array secondPart 
       var secondPart=allParts[1].split("=");       // secondPart[0]="sName", secondPart[1]="Smith" 
     
    // show result of splitting
       alert("You entered "+firstPart[1]+" "+secondPart[1]) 
     }
    // ---------------
     
    //-->  
      </script>
    </head>
     
    <body onload="init()">
     
    <form name="SWnamefunc">
      <div>
        <!-- changed input type from "hidden" to "text" so that you can see the result -->
        <p class="a"><input TYPE="text" NAME="yoyo" SIZE="100"></div>
      <!-- end div -->
    </form>
    <div id="info">
    </div>
    <!-- end info -->
     
    </body>
     
    </html>

  4. #4
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Thanks so much for your help!

    I have now figured out the how to split the url info into two names.

    I now need to work out how to send the info to the php script. I am trying to post the name, described as the variable "firstlastname" to a php script that then emails me the information through the following hidden input tag, embedded in a form:

    <form action="feedback.php" method="post">

    <input type="hidden" name="teacher" value='<? php echo $firstlastname;?>' />
    ....

    The problem is that I cannot seem to be able to link the name, which varies depending on a name clicked on the previous page (and shows up therefore in the url). I have tried a few different variations and seem to only get "firstlastname" showing up on the email, and not the actual name of the teacher. Ie., this is currently not recognised by the php script as a variable and only shows up as a fixed value. This is very annoying. The other part of the script, that directs the html page to show the name as separate first and last names [with a document.write(firstlastname)] seems to be working, so I figure that there is a problem with the <input type...> line as shown above.

    I have defined the value firstlastname above using the following line earlier on in the script and have also defined the delineate 1 and 2 functions. These all seem to work well.

    var firstlastname = delineate1(text)+' '+delineate2(text);

    How can I fix this so that I get the value for the firstlastname and not get the script to post "firstlastname" to the email?

    Any help would be much appreciated.


Bookmarks

Posting Permissions

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