Page 1 of 1

Global Variable || POST problem

Posted: Tue Jan 20, 2004 4:58 pm
by neophyte
Okay I'm working on "a share this article" script. It's unique because I'm dynamically passing the url from page one to page two.

This is one page one

Code: Select all

<form action="SendArticle2.php" method="post" name="alice">	  
  <input type="SUBMIT"  value="Submit">
 <script language="JavaScript">var theSpot=document.URL; document.write("<input type='hidden' name='place' value='"+theSpot+"'>"); document.write(theSpot);</script>
 </form>
As you can see I'm using Java Script to pass the variable to page two.
(I tried using "REFERER" on page two but I couldn't get it to work. But that's another story. If your interested I'll tell you about it)


Okay so "the Spot" is passed on to page two like this :

Code: Select all

<?php 
/* validation.php */ 

require_once ('functions.php'); 

$valid = TRUE; 

$whatEver = $_POST['place']; 
$monkey = $whatEver; 
echo "$monkey";
I know that the variable is carried over because $monkey prints on the page. Below that is this:

Code: Select all

if (isset ($_POST['submit']) { 
foreach($_POST as $key=>$value) { 
$$key = $value; 
} 

$valid = $rn = checkLength($rname, 1, 30); 
$rem = checkEmail($remail); 
$valid = $valid && $rem; 
$sn = checkLength($sname, 1, 30); 
$valid = $valid && $sn; 
$sem = checkEmail($semail); 
$valid = $valid && $sem; 
if ($valid) { 

sendArticle(); 


} 
} else { 
$rn = $sn = $sem = $rem = TRUE; 
$rname = $sname = $semail = $remail; 
}
And Here's the mail function. My problem is that declaring globals isn't working to get the $monkey/$whatEver into the Email. I can't figure out why?

Code: Select all

function sendArticle(){ 

global $whatEver, $monkey, $rname, $remail, $sname, $semail, $comment; 

$origin=$_SERVER["HTTP_REFERER"]; 
$Message = "Hello,\n$sname, thought you would be interested in this article at rwyouth.com \n\n $monkey \n\n Comment: $comment"; 
$ToSubject = "$rname, I found this great article at RWYouth.com."; 
echo "<span class="boldblack">Your Email Was Sent! $monkey <br> Send Another?</span>"; 
echo "$monkey"; 
mail($rname." <".$remail.">",$ToSubject, $Message, "From: ".$sname." <".$semail.">"); 

}

THEY ALWAYS DO THIS WHEN I"M TRYING TO POST!--DIE APPLE

Posted: Tue Jan 20, 2004 5:01 pm
by neophyte
If anybody out there knows why I can print $monkey on page 1 but can't get it to the email I'd love to here it...


Newbie

Posted: Tue Jan 20, 2004 5:03 pm
by neophyte
Sorry about all the breaks and the lack of formatting on my posts... I'm on a MAC (E-gad) and it won't let me format anything. :cry:


Sorry

NEWBIE

Posted: Tue Jan 20, 2004 5:21 pm
by markl999
Your submit button is called 'Submit' but you have :
if (isset ($_POST['submit'])){
It should be :
if (isset ($_POST['Submit'])){

Also, instead of doing :
foreach($_POST as $key=>$value) {
$$key = $value;
}
you could just do :
extract($_POST);

Posted: Tue Jan 20, 2004 6:09 pm
by infolock
well, #1, why are you seeing if submit is set? submit is just a button.. so of course it's gonna be active. the best method is to check if a field is set since that is what makes your program work.

secondly, you are redefining variables over and over and over again. why? there seems no clear reasoning behind this.

third, do you have globals turned on in your php configuration?

fourth, tell us what your script is suposed to do. i think you entire script needs to be rewritten personally.

Wow you guys are harsh

Posted: Tue Jan 20, 2004 9:15 pm
by neophyte
I'm such a dingleberry. The second page form submits to itself. All I had to add was this:
<input type="hidden" name="place" value="<?= $monkey ?>">
The rest is self explanatory.

My script might be ugly but it is working.

The code

Posted: Tue Jan 20, 2004 9:54 pm
by neophyte
It's just a little script I wrote to "share this article". If any of you have suggestions on how it could have been written differently or 'mo betta'
I'm all ears.

Page one is an HTML page. It submits to page two.

Code: Select all

&lt;form action="SendArticle2.php" method="post"&gt;	  
  &lt;input type="SUBMIT"  value="Submit"&gt;
 &lt;script language="JavaScript"&gt;var theSpot=document.URL; document.write("&lt;input type='hidden' name='place' value='"+theSpot+"'&gt;"); document.write(theSpot);&lt;/script&gt;
 &lt;/form&gt;

Code: Select all

<?php


require_once ('functions.php');

$valid = TRUE;

$whatEver = $_POST['place'];
$monkey = $whatEver;


if (isset ($_POST['submit'])) {
    foreach($_POST as $key=>$value) {
      $$key = $value;
    }
    
      $valid = $rn = checkLength($rname, 1, 30);
    $rem = checkEmail($remail);
    $valid = $valid && $rem;
	$sn = checkLength($sname, 1, 30);
    $valid = $valid && $sn;
	$sem = checkEmail($semail);
    $valid = $valid && $sem;
    if ($valid) {
	
   sendArticle();
	
		
    }
} else {
    $rn = $sn = $sem =  $rem =  TRUE;
    $rname = $sname = $semail = $remail;
}


function sendArticle(){

global $whatEver, $monkey, $rname, $remail, $sname, $semail, $comment;
	
	
       $Message = "Hello,\n$sname, thought you would be interested in this article at yourdomain.com \n\n $monkey \n\n Comment: $comment";
		$ToSubject = "$rname, I found this great article at RWYouth.com.";
		 echo "<span class="boldblack">Your Email Was Sent!  <br> Send Another?</span>";
		
        mail($rname." <".$remail.">",$ToSubject, $Message, "From: ".$sname." <".$semail.">");

}


?>

<?php
 
//$requested_uri = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];  
//echo "$requested_uri";
?>
<?php
if (!$valid) {
?>
<style type="text/css">
td.error {color:C03; font-weight:bold; }
</style>
Please correct the items in orange and resubmit.
<br />
<?php
}
?>
<form method="POST" action="" >
  <table border="0" cellpadding="0" cellspacing="0">
    <tr> 
      <td align="left"<?php if(!$rn){ echo 'class="boldborange"';} else { echo 'class="boldblack"';} ?>> 
        Friend's Name:</td>
    </tr>
    <tr> 
      <td> <input type="text" name="rname" size="15" value="<?= $rname ?>"> </td>
    </tr>
    <tr> 
      <td align="left"<?php if(!$rem){ echo 'class="boldborange"';} else { echo 'class="boldblack"';} ?>> 
        Friend's Email:</td>
    </tr>
    <tr> 
      <td> <input type="text" name="remail" size="15" value="<?= $remail ?>"> 
      </td>
    </tr>
    <tr> 
      <td align="left"<?php if(!$sn) { echo 'class="boldborange"';} else { echo 'class="boldblack"';}?>> 
        Your Name: </td>
    </tr>
    <tr> 
      <td> <input type="text" name="sname" size="15" value="<?= $sname ?>"> </td>
    </tr>
    <tr> 
      <td align="left"<?php if(!$sem) { echo 'class="boldborange"';} else { echo 'class="boldblack"';} ?>> 
        Your Email:</td>
    </tr>
    <tr> 
      <td> <input type="text" name="semail" size="15" value="<?= $semail ?>"> 
      </td>
    </tr>
    <tr> 
      <td align="left" valign="top" ><span class="boldblack">Comments:</span></td>
    </tr>
    <tr> 
      <td> <textarea name="comment" cols="15" rows="10"><?= $comment ?></textarea><input type="hidden" name="place" value="<?= $monkey ?>"> 
      </td>
    </tr>
    <tr> 
      <td> <input type="SUBMIT" name="submit" value="Submit"> 
      </td>
    </tr>
  </table>