Submission of URL variable problem

  • Travis Coats
  • Graduate
  • Graduate
  • Travis Coats
  • Posts: 107

Post 3+ Months Ago

Yet again I am try to polish up the fetures on the site, and this one has been bugging me for quite some time. I use Bluehost to host the site and they offer a very easily integrated mailing system called blue mail. Here's the problem I am facing:

Code: [ Select ]
<form action="http://www.bluehost.com/bluemail" method="post" enctype="multipart/form-data" name="emailForm" id="emailForm" onsubmit="getName()">
        <input name="redirect" type="hidden" value="http://www.allinnia.com/contact.php?name=<?php echo $_POST['name']; ?>" />
        <input name="subject" type="hidden" value="Site Contact" />
     <input name="sendtoemail" type="hidden" value="blurred for spammer hatred" />
     <input name="text" type="hidden" value="1" />
     <table width="100%" border="0" cellpadding="2" cellspacing="5" class="tableBackgroundHighlightBlue">
        <tr>
         <td width="14%"><div align="right"><label for="name">Name</label></div></td>
         <td><span id="spryNameTxt">
            <input name="name" type="text" class="textInputLightGrey" id="name" size="30" />
            <span class="textfieldRequiredMsg">A value is required.</span></span></td>
         </tr>
        
        <tr>
         <td><div align="right"><label for="email">Email</label></div></td>
         <td><span id="spryEmailTxt">
         <input name="email" type="text" class="textInputLightGrey" id="email" size="30" />
         <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
        </tr>
        <tr>
         <td><div align="right"><label for="message">Message</label></div></td>
         <td valign="top"><span id="spryMessageTxt">
            <textarea name="message" cols="30" rows="5" class="textInputLightGrey" id="message"></textarea>
            <span class="textareaRequiredMsg">A value is required.</span></span></td>
        </tr>
        <tr>
         <td>&nbsp;</td>
         <td><input name="emailBtn" type="submit" class="buttonBlack" id="emailBtn" value="Send Email" /></td>
        </tr>
     </table>
     </form>
  1. <form action="http://www.bluehost.com/bluemail" method="post" enctype="multipart/form-data" name="emailForm" id="emailForm" onsubmit="getName()">
  2.         <input name="redirect" type="hidden" value="http://www.allinnia.com/contact.php?name=<?php echo $_POST['name']; ?>" />
  3.         <input name="subject" type="hidden" value="Site Contact" />
  4.      <input name="sendtoemail" type="hidden" value="blurred for spammer hatred" />
  5.      <input name="text" type="hidden" value="1" />
  6.      <table width="100%" border="0" cellpadding="2" cellspacing="5" class="tableBackgroundHighlightBlue">
  7.         <tr>
  8.          <td width="14%"><div align="right"><label for="name">Name</label></div></td>
  9.          <td><span id="spryNameTxt">
  10.             <input name="name" type="text" class="textInputLightGrey" id="name" size="30" />
  11.             <span class="textfieldRequiredMsg">A value is required.</span></span></td>
  12.          </tr>
  13.         
  14.         <tr>
  15.          <td><div align="right"><label for="email">Email</label></div></td>
  16.          <td><span id="spryEmailTxt">
  17.          <input name="email" type="text" class="textInputLightGrey" id="email" size="30" />
  18.          <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
  19.         </tr>
  20.         <tr>
  21.          <td><div align="right"><label for="message">Message</label></div></td>
  22.          <td valign="top"><span id="spryMessageTxt">
  23.             <textarea name="message" cols="30" rows="5" class="textInputLightGrey" id="message"></textarea>
  24.             <span class="textareaRequiredMsg">A value is required.</span></span></td>
  25.         </tr>
  26.         <tr>
  27.          <td>&nbsp;</td>
  28.          <td><input name="emailBtn" type="submit" class="buttonBlack" id="emailBtn" value="Send Email" /></td>
  29.         </tr>
  30.      </table>
  31.      </form>


The simple part is that the form is set to post the variables to Bluemail then using hidden fields bluemail redirects the browser back to whatever page I tell it. I used to have it go to another page (the listener page), but I always wanted to add a little confirmation message on the contact page after submission. With my new found exploration of php and javascript I thought I could concoct a solution to this, which I did partly. I have this script to echo a string of text which includes the person's name:

Code: [ Select ]
<?php
$name = $_GET['name'];
if ($name == "")
{
echo "";
}
else
{
echo "Thanks $name , you will receive a response soon";
}
?>
  1. <?php
  2. $name = $_GET['name'];
  3. if ($name == "")
  4. {
  5. echo "";
  6. }
  7. else
  8. {
  9. echo "Thanks $name , you will receive a response soon";
  10. }
  11. ?>

This code is located right above the form, if you test it you will see it working just fine by typing a URL parameter in: http://www.allinnia.com/contact.php?name=type something here. I need to retrieve the name variable from this form, but the redirect is nullifying the effects of the script. I even tried a similar javascript function to be submitted with the form as an onsubmit command. Is there an easier solution to fix this problem. If you send the form it will be reset by the redirect, but even with the code:

Code: [ Select ]
<input name="redirect" type="hidden" value="http://www.allinnia.com/contact.php?name=<?php echo $_POST['name']; ?>" />


The variable is not being passed....this is over my head, but I am sure is a cakewalk for some of you guys.
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Your problem stems from not understanding how requests and server-side languages work. Your value of $_POST['name'] which you use as a hidden field value will always be empty. Why? Because nothing sets it before the form is submitted to the bluemail script. PHP is a server-side language, meaning that unlike JavaScript, a client-side language, it's not monitoring the browser for changes. So, PHP can't look at the name field, see that it's been filled out, then add that value to the hidden field before the form is selected. The $_POST array isn't even filled until form submission. Upon submission, $_POST['redirect'] will always equal "http://www.allinnia.com/contact.php?name=".

Your actual name script outputs values sent through the URL because that's how the $_GET array works. Its values are filled with those items sent through a query string (i.e., the part after the '?' in your redirect URL). You could put anything into the 'name= ' part, and it would echo to the screen. So that, in and of itself, isn't an indication of the redirect working or not working because it's not tied to the form submission process.

Unless you can get your hands on the bluemail script itself, your only efficient option would be to use AJAX to fill that hidden field value. With the help of a good JavaScript framework, it won't be too hard to do. Let me know if you want me to walk you through it.
  • Travis Coats
  • Graduate
  • Graduate
  • Travis Coats
  • Posts: 107

Post 3+ Months Ago

You are absolutely correct - I am a beginner. :)

The bluemail script allows for a redirect to be made, but the example Bluehost says to follow calls for results to be posted (not in the browser window). I figured that the value wasn't carrying over - even if I were to use a post. The name variable isn't a hidden field value though, it is the first text box value sent to bluehost. I thought maybe I could intercept and store it some how and pass it on to the redirect hidden field. As you say though, that's not how it works...I was stabbing in the dark.

I would love to hear your Ajax suggestion, I am very new to javascript, but I learn fast and dreamweaver is a very helpful tool for making it happen and seeing the process. The goal is to give the submitter an assurance of what he/she submitted. If I can get this to work I will be applying it to other forms on the site as well.
  • Travis Coats
  • Graduate
  • Graduate
  • Travis Coats
  • Posts: 107

Post 3+ Months Ago

RichB, you are an excellent man! For those of you who may need something similar to this on your site here's the solution that Rich came up with - and it works perfectly!

Code: [ Select ]
 
<script type="text/javascript">
<!--
function getName(){
    var theName = document.getElementById("name").value;
    var theUrl = "http://www.allinnia.com/contact.php?name=" + theName;
    document.getElementById("redirect").value = theUrl;
    return true;
 }
 // -->
</script>
 
  1.  
  2. <script type="text/javascript">
  3. <!--
  4. function getName(){
  5.     var theName = document.getElementById("name").value;
  6.     var theUrl = "http://www.allinnia.com/contact.php?name=" + theName;
  7.     document.getElementById("redirect").value = theUrl;
  8.     return true;
  9.  }
  10.  // -->
  11. </script>
  12.  


My Form Code:
Code: [ Select ]
<form action="http://www.bluehost.com/bluemail" method="post" enctype="multipart/form-data" name="emailForm" id="emailForm" onsubmit="return getName()">
        <input name="redirect" id="redirect" type="hidden" value="" />
        <input name="subject" type="hidden" value="Site Contact" />
     <input name="sendtoemail" type="hidden" value="me@mydomain.com" />
     <input name="text" type="hidden" value="1" />
     <table width="100%" border="0" cellpadding="2" cellspacing="5" class="tableBackgroundHighlightBlue">
        <tr>
         <td width="14%"><div align="right"><label for="name">Name</label></div></td>
         <td><span id="spryNameTxt">
            <input name="name" type="text" class="textInputLightGrey" id="name" size="30" />
  1. <form action="http://www.bluehost.com/bluemail" method="post" enctype="multipart/form-data" name="emailForm" id="emailForm" onsubmit="return getName()">
  2.         <input name="redirect" id="redirect" type="hidden" value="" />
  3.         <input name="subject" type="hidden" value="Site Contact" />
  4.      <input name="sendtoemail" type="hidden" value="me@mydomain.com" />
  5.      <input name="text" type="hidden" value="1" />
  6.      <table width="100%" border="0" cellpadding="2" cellspacing="5" class="tableBackgroundHighlightBlue">
  7.         <tr>
  8.          <td width="14%"><div align="right"><label for="name">Name</label></div></td>
  9.          <td><span id="spryNameTxt">
  10.             <input name="name" type="text" class="textInputLightGrey" id="name" size="30" />


I bolded the variables, these names will change to suit your needs, but the functionality basically checks the input value of a textbox, in this case id=name, and then populates this dynamic data onsubmit by calling the getName function, which then populates the hidden field value to redirect to the intended page and appends the variable "theName"(which is the text input id=name, to the end of the URL.

I would like to publicly thank Rich for this code, it's not often you find guys so willing to help little guys like myself. Thanks Rich! :D

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: Liamw411 and 69 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.