Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Simple PHP $_POST problem - driving me nuts!!!
Old 09-04-2010, 06:48 AM Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 7
Name: seen
Trades: 0
Hi guys,
I posted this a couple of days ago but have had no luck so far so I thought I'd post the whole code to see if anyone can possibly see whats going wrong here. I was about to give up and use javascript but that will probably just bring up more problems!

Any help would be very very much appreciated!!!

Here's the code for the first page:
(I've removed some database stuff but I know half of this page is working as the redirect works fine??)

Code:
<?php require_once('Connections/database.php'); ?>
<?php 
$your_email ='my email address';

session_start();
$errors = '';
$name = '';
$visitor_email = '';
$phone = '';
$city = '';
$interest = '';
$hear = '';
$newsletter = '';
$score = 0;

if(isset($_POST['submit']))
{	
	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$phone = $_POST['phone'];
	$city = $_POST['city'];
	$interest = $_POST['interest'];
	$hear = $_POST['hear'];
	$newsletter = $_POST['newsletter'];
	
	for($i=0;$i<11;$i++){
	$answer = $_POST['radioq'.$i];
	$score += $answer;
	}
		
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email)||empty($phone)||empty($city))
	{
		$errors .= "\n Name, Email, Phone and City are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
	if(empty($_SESSION['6_letters_code'] ) ||
	  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
	//Note: the captcha code is compared case insensitively.
	//if you want case sensitive match, update the check above to
	// strcmp()
		$errors .= "\n The captcha code does not match!";
	}

	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="form submission";
		$from = $your_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

		$body = "A user called $name submitted the form:\n".
		"Name: $name\n".
		"Email: $visitor_email \n".
		"Contact number: $phone\n".
		"City: $city\n".
		"$score \n".
		"\n";	
		
		$headers = "From: $from \r\n";
		$headers .= "Reply-To: $visitor_email \r\n";
		
		mail($to, $subject, $body, $headers);
		
if ($score > 15) {
    	header('location: next-page.php?id=4');
} else if ($score > 11) {
    	header('location: next-page.php?id=3');
} else if ($score > 5) {
    	header('location: next-page.php?id=2');
} else {
    	header('location: next-page.php?id=1');
	}
}
}

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
And the code on the second page which should show the result:

<h1>Here are your results</h1>
<span style="font-size:3em;"><?php echo $_POST['score']; ?></span>

I think the problem is that the first page has the
Code:
if(isset($_POST['submit'])){
line which is causing the values not to be apssed to the next page? Does anyone know how I can keep the value $score available into the next page to display the score? I'm sooo stuck!! My 3rd day on this and I've read countless articles and tried different options and I'm still stuck. Please help!!
koldhands is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-04-2010, 07:34 AM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 12
Name: chris
Trades: 0
im looking at the code and specifically the

PHP Code:
if(isset($_POST['submit'])){

 $ 
your collection vars


is the form submit button set like this?
HTML Code:
<input type='submit' value='submit' />
if so that is your problem, there needs to be a name attribute to the input type submit if your checking it is submitted in this way.

Another method a little more code but would also lead to some more checks and you could obviously do some form returns with it. would be to check each post individually.



EG
HTML Code:
<input type='submit' name='myform' value='submit' />
would reflect this

PHP Code:
if(isset($_POST['myform'])){

 $ 
your collection vars


I dont know that this is your problem and without seeing the form that you use to submit the data i can only conclude an assumption as to your form

Last edited by cdjhosting; 09-04-2010 at 07:40 AM.. Reason: typo
cdjhosting is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 09:41 AM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 7
Name: seen
Trades: 0
Hi chris,

thanks for the reply. I have a button with the following code:

Code:
<input name='submit' type="submit" class="submitBtn" value="Submit">
So it seems my if(isset($_POST['submit'])){ maybe correct?

I was thinking maybe I could include a hidden field to retain the values across the 2 pages?

Thanks again.
koldhands is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 01:11 PM Re: Simple PHP $_POST problem - driving me nuts!!!
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Hey,

why would you use hidden fields when you already have session started?
Just read those values in a session var and they'll be there till the session expires.

Form processing:

PHP Code:
$_SESSION['var1'] = $_POST['value1'];
$_SESSION['var2'] = $_POST['value2']; 
After that you can simply use them like this:

PHP Code:
echo $_SESSION['var1'];
echo 
$_SESSION['var2']; 
I noticed one more thing:
PHP Code:
<?php require_once('Connections/database.php'); ?>
Avoid using capital letters and spaces for folder and file names

Last edited by miki86; 09-04-2010 at 01:16 PM..
miki86 is online now
Reply With Quote
View Public Profile
 
Old 09-04-2010, 02:46 PM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 7
Name: seen
Trades: 0
Thanks so much Miki.
Just reading a chapter on sessions now! And yeah you're thinking I should've done that yonks ago but thats what happens when you get reliant on software to do things for ya!

And cheers for the other tip - although again, DW insists on naming the Connections folder with a big 'C'.

Ta!
koldhands is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 02:54 PM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 12
Name: chris
Trades: 0
you dont need to redefine as session variables, ofcourse that is one option but if you are able to define a session vaiable from the $_POST then the problem lies else where and you will need to look into it further.
cdjhosting is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 02:56 PM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 12
Name: chris
Trades: 0
Quote:
Originally Posted by koldhands View Post
Hi chris,

thanks for the reply. I have a button with the following code:

Code:
<input name='submit' type="submit" class="submitBtn" value="Submit">
So it seems my if(isset($_POST['submit'])){ maybe correct?

I was thinking maybe I could include a hidden field to retain the values across the 2 pages?

Thanks again.
Try adding a / at the end of the submit button, i know its a small matter and it really shouldnt make a difference but it can't help trying close the button like so />
cdjhosting is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 03:39 PM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 7
Name: seen
Trades: 0
Well I tried both suggestions above and nothing seems to have worked
I added some code to see what the SESSION and POST were holding after the user is sent to the second page by adding the following code:

<?php
session_start();

echo "<pre>POST array contains: ";
print_r($_POST);
echo "</pre>";

echo "<pre>Session contains: ";
print_r($_SESSION);
echo "</pre>";
?>

This gave me the following results:

Session contains: Array
(
[6_letters_code] => dcrjgf (This is just the captcha code)
[score] => None set
)

The score not being set is still the problem here and I'm wondering where I'm going wrong??

I tried adding $_SESSION['finalscore'] = $_POST['score']; to both pages just in case (is that what you meant miki?) and then tried to ECHO the finalscore variable. Still no luck!
Any more suggestions guys and gals?
koldhands is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 03:48 PM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 7
Name: seen
Trades: 0
At last!

Finally sorted it thanks to miki's suggestions about the sessions.

It was a rookie error, not starting the session again on the second page - back to the PHP guides over the weekend then!

Thanks guys for all your help and suggestions.
koldhands is offline
Reply With Quote
View Public Profile
 
Old 09-04-2010, 04:22 PM Re: Simple PHP $_POST problem - driving me nuts!!!
Novice Talker

Posts: 12
Name: chris
Trades: 0
Quote:
Originally Posted by koldhands View Post
At last!

Finally sorted it thanks to miki's suggestions about the sessions.

It was a rookie error, not starting the session again on the second page - back to the PHP guides over the weekend then!

Thanks guys for all your help and suggestions.
its not entirely a rookie error, the $_POST should have transfered to the new page on submit thats the point of them,
cdjhosting is offline
Reply With Quote
View Public Profile
 
Old 09-05-2010, 08:03 AM Re: Simple PHP $_POST problem - driving me nuts!!!
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Yeah you need to start the session on every page that is using session variables, and start it before you input any html, at the very top.
miki86 is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to Simple PHP $_POST problem - driving me nuts!!!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.82360 seconds with 12 queries