styler Posted March 26, 2009 Share Posted March 26, 2009 Hi, I have been trying to create a page where the php sits on the same page as the form it receives data from. I am hoping to the echo out the results to the same page once the php has been run: I just can't seem to get the code to work: <?php if(isset($_POST['submit'])) { $email = $_POST['email']; $to = "[email protected]"; $subject = "Newsletter Subscription "; $message = ("email of sender: ".$email); $headers = "From:Improvement Associates\r\n"; if(mail($to,$subject,$message,$header)) { $result = "Many thanks for subscribing to our newsletter, you have been added to the mailing list"; } else { $result = "<h3>Error sending your message.</h3> Sorry, an error occured sending.<br />Please try again later or contact us direct to be added to the mailing list"; }) ?> <html><body> <div id="forminp" class="marg"> <form method="post" action="<? $_SERVER['PHP_SELF']; ?>"> <label for="email">email*</label> <input type="text" class="textinput" size="33" tabindex="2" name="email" id="email"/> <div class="submit"><input id="button" name="submit" type="submit" value="submit" /></div> <div class="formfoot"> <p id="req">* required</p> </div> </form> <?php print $result; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/ Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 There's two different styles: </pre> <form method="post" action="<?=%24_SERVER%5B'PHP_SELF'%5D;%20?>">< </pre> <form method="post" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>">< Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794609 Share on other sites More sharing options...
vicodin Posted March 26, 2009 Share Posted March 26, 2009 Here is your problem... <form method="post" action="<? $_SERVER['PHP_SELF']; ?>"> Change it to this <form method="post" action="<?=$_SERVER['PHP_SELF']; ?>"> Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794612 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Is there an echo in here? Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794613 Share on other sites More sharing options...
WolfRage Posted March 26, 2009 Share Posted March 26, 2009 lol, I won't echo, got beat to it. Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794620 Share on other sites More sharing options...
styler Posted March 26, 2009 Author Share Posted March 26, 2009 many thanks, but it still isn't working - i think there must be a problem with the php code as i'm getting nothing in the browser.. Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794639 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Replace the ')' with '}'. You weren't ending the isset for the submission. $result = "Error sending your message. Sorry, an error occured sending. Please try again later or contact us direct to be added to the mailing list"; }} ?> Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794642 Share on other sites More sharing options...
styler Posted March 26, 2009 Author Share Posted March 26, 2009 that's got it - thanks guys. Link to comment https://forums.phpfreaks.com/topic/151268-form-php-in-the-same-page/#findComment-794645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.