#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Converting variable values into strings


    Below is the code that I am trying to implement. everything is working fine except the email that is to be sent via PHP when a user completes the sign up process on a webpage.

    We are trying to get the country code information into the email message that will be sent to us however here is what we get.

    When we tried to put in $message = "Country Code: ".$_POST["countrycode"];

    We get the following: Country Code:

    When I tried to put in $message = "Country Code: ".'<script>document.write(geoip_country_code());</script>'."\n";

    We get the following: Country Code: <script>document.write(geoip_country_code());</script>

    I am not sure how to take the value that is stored in $countrycode = "<script>document.write(geoip_country_code());</script>"."\n"; and then convert it to a value that I can push into the $message just as it's shown on the browser. For Example:

    Your Information

    Country Code: US
    Country Name: Unites States
    City: new York
    Region: NY
    Region Name: New York
    Mail Sent.

    Any help would be greatly appreciated. The code is below:


    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <script language='javascript'>
    var veg = 'Carrot';
    </script>
    <script type="text/javascript" src="(The API link )"></script>
    <body>
    <?php

    $countrycode = "<script>document.write(geoip_country_code());</script>"."\n";
    $countryname = "<script>document.write(geoip_country_name());</script>"."\n";
    $city = "<script>document.write(geoip_city());</script>"."\n";
    $region = "<script>document.write(geoip_region());</script>"."\n";
    $regionname = "<script>document.write(geoip_region_name());</script>"."\n";
    echo 'Your Information';
    echo '<br><br>Country Code: '.$countrycode;
    echo '<br>Country Name: '.$countryname;
    echo '<br>City: '.$city;
    echo '<br>Region: '.$region;
    echo '<br>Region Name: '.$regionname;

    $to = "email address";
    $subject = "Sign Up Location";
    $message = "Country Code: ".$_POST["countrycode"];
    $from = "email address";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "<br>Mail Sent.";
    ?>

    </body>
    </html>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    You cannot call Javascript from your PHP code.

    a) Do the GeoIP stuff in your PHP too/instead
    b) Use Javascript to get the GeoIP into the form being submitted, as hidden inputs, and access them in your PHP via $_POST like normal
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    You cannot call Javascript from your PHP code.

    a) Do the GeoIP stuff in your PHP too/instead
    b) Use Javascript to get the GeoIP into the form being submitted, as hidden inputs, and access them in your PHP via $_POST like normal
    Thank you requinix

    Can you please give me an example of how I would be able to use Javascript to get the GeoIP into the form being submitted, as hidden inputs?

    Thanks again for all your help.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    Gotta love copying and pasting replies...

    What's the code for your form?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    Gotta love copying and pasting replies...

    What's the code for your form?
    Sorry about Copy / paste for reply but it was faster

    In regards to the form, we don't have one. What I am trying to do is pull the data from the values from:

    <?php

    $countrycode = "<script>document.write(geoip_country_code());</script>"."\n";
    $countryname = "<script>document.write(geoip_country_name());</script>"."\n";
    $city = "<script>document.write(geoip_city());</script>"."\n";
    $region = "<script>document.write(geoip_region());</script>"."\n";
    $regionname = "<script>document.write(geoip_region_name());</script>"."\n";
    echo 'Your Information';
    echo '<br><br>Country Code: '.$countrycode;
    echo '<br>Country Name: '.$countryname;
    echo '<br>City: '.$city;
    echo '<br>Region: '.$region;
    echo '<br>Region Name: '.$regionname;

    $to = "email address";
    $subject = "Sign Up Location";
    $message = "Country Code: ".$_POST["countrycode"];
    $from = "email address";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "<br>Mail Sent.";
    ?>

    For example, I would like to take the actual value from $countrycode and push that into an email that is sent using PHP.
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    No form? Then what about
    PHP Code:
    $message "Country Code: ".$_POST["countrycode"]; 
    Emails shouldn't be triggered just by viewing a page. There needs to be some user-driven event, and that event is just about always a form submission.
    I guess my question is this: what does the user do to make this email get sent?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    No form? Then what about
    PHP Code:
    $message "Country Code: ".$_POST["countrycode"]; 
    Emails shouldn't be triggered just by viewing a page. There needs to be some user-driven event, and that event is just about always a form submission.
    I guess my question is this: what does the user do to make this email get sent?
    thanks again requinix for taking the time to help us with this. we are just in the initial phase of testing this and you are right, the page would not trigger the email, we were just testing the sending email and including variables within the email.

    To make things simple though as we don't have a completed form yet, i copied a form code from the HeadFirstPHPMySQL book and included it below. Are you able to show me an example of how to include the variables within the form with this information?


    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Aliens Abducted Me - Report an Abduction</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <h2>Aliens Abducted Me - Report an Abduction</h2>

    <p>Share your story of alien abduction:</p>
    <form method="post" action="report.php">
    <label for="firstname">First name:</label>
    <input type="text" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" name="lastname" /><br />
    <label for="email">What is your email address?</label>
    <input type="text" name="email" /><br />
    <label for="whenithappened">When did it happen?</label>
    <input type="text" name="whenithappened" /><br />
    <label for="howlong">How long were you gone?</label>
    <input type="text" name="howlong" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" name="howmany" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" name="aliendescription" size="32" /><br />
    <label for="whattheydid">What did they do to you?</label>
    <input type="text" name="whattheydid" size="32" /><br />
    <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input name="fangspotted" type="radio" value="yes" />
    No <input name="fangspotted" type="radio" value="no" /><br />
    <img src="fang.jpg" width="100" height="175"
    alt="My abducted dog Fang." /><br />
    <label for="other">Anything else you want to add?</label>
    <textarea name="other"></textarea><br />
    <input type="submit" value="Report Abduction" name="submit" />
    </form>
    </body>
    </html>
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    No form? Then what about
    PHP Code:
    $message "Country Code: ".$_POST["countrycode"]; 
    Emails shouldn't be triggered just by viewing a page. There needs to be some user-driven event, and that event is just about always a form submission.
    I guess my question is this: what does the user do to make this email get sent?
    There is no data being passed in the email when we tried $message = "Country Code: ".$_POST["countrycode"];

    We get the following: Country Code:


  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    There's no input in you form with the name "countrycode" so of course you won't get anything in your PHP _POST arary for it. That much should be commone sense - you can access something that wasn't given to you.

    What you can do is to create hidden inputs in your form. These will get submitted and you can put javascript generated data in these.

    eg

    Code:
    <input type='hidden' name='countrycode' id='countrycode' value='' />
    you can then use

    Code:
    <script>
    element = document.getElementById('countrycode');
    element.value = geoip_country_code();
    </script>
    to set the value

    When the page loads, the input is hidden (and remains so) but has an empty value.
    Almost instantly the javascript will execute and set the value attribute of the input to be the value returned by geoip_country_code()
    When you submit the form, the value is sent and will be available in $_POST['countrycode']

    You won't see this change take place in the html source by doing view source, you'll have to use some live inspector such as firebug (a firefox plugin) or the web developer toolbar's "view generated source" button
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Northie
    There's no input in you form with the name "countrycode" so of course you won't get anything in your PHP _POST arary for it. That much should be commone sense - you can access something that wasn't given to you.

    What you can do is to create hidden inputs in your form. These will get submitted and you can put javascript generated data in these.

    eg

    Code:
    <input type='hidden' name='countrycode' id='countrycode' value='' />
    you can then use

    Code:
    <script>
    element = document.getElementById('countrycode');
    element.value = geoip_country_code();
    </script>
    to set the value

    When the page loads, the input is hidden (and remains so) but has an empty value.
    Almost instantly the javascript will execute and set the value attribute of the input to be the value returned by geoip_country_code()
    When you submit the form, the value is sent and will be available in $_POST['countrycode']

    You won't see this change take place in the html source by doing view source, you'll have to use some live inspector such as firebug (a firefox plugin) or the web developer toolbar's "view generated source" button
    I tried to implement the following but it doesn't seem to be working.

    The first file is

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    </script>
    <script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
    <script>
    function location(){
    element = document.getElementById('countrycode');
    element.value = geoip_country_code();
    element = document.getElementById('countryname');
    element.value = geoip_country_name();
    element = document.getElementById('city');
    element.value = geoip_city();
    element = document.getElementById('region');
    element.value = geoip_region();
    element = document.getElementById('regionname');
    element.value = geoip_region_name();
    }
    </script>

    <form action="firsttest.php" method="post">
    <input type='hidden' name='countrycode' id='countrycode' value='' />
    <input type='hidden' name='countryname' id='countryname' value='' />
    <input type='hidden' name='city' id='city' value='' />
    <input type='hidden' name='region' id='region' value='' />
    <input type='hidden' name='regionname' id='regionname' value='' />
    <input name="submit" type="submit" onclick="location()"
    value="Show Me Output"></form>

    </body>
    </html>

    Then the following code gets called:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php

    $countrycode = $_POST["countrycode"]."\n";
    $countryname = $_POST["countryname"]."\n";
    $city = $_POST["city"]."\n";
    $region = $_POST["region"]."\n";
    $regionname = $_POST["regionname"]."\n";
    echo 'Your Information';
    echo '<br><br>Country Code: '.$countrycode;
    echo '<br>Country Name: '.$countryname;
    echo '<br>City: '.$city;
    echo '<br>Region: '.$region;
    echo '<br>Region Name: '.$regionname;

    $to = "email@emailaddress.com";
    $subject = "Sign Up Location";
    $message = "Country Code: ".$_POST["$countrycode"];
    $from = "email@emailaddress.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "<br>Mail Sent.";
    ?>
    </body>
    </html>


    Within the email I only implemented the $message = "Country Code: ".$_POST["$countrycode"];
    for testing but it still comes back with no information.

    When the webpage comes up, there is no data either.

    Can you please let me know what I may be doing wrong?

    Thanks again for all your help.
  20. #11
  21. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    without a proper inspection my first guess is that either my code is wrong (I wrap everything up in jQuery's $(function(){/*....*/}) method for ease) or you have a race condition - there is no need to wait for the onclick (which should probably be an onsubmit in the form) to load the values - you can do it as soon as the html is ready (that's why we put scripts and script tags at the end of the html and not in the head tag)
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo