session not getting value from one php value to another - php

I am trying to send value of a variable named "topic" from one php file to another php file ... there is no error in the code but the value of variable is not displaying in the other php file ...
please help ... thank you
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$topic = $_POST['topic'];
$message = "From: ".$name."\r\n";
$message .= $_POST['message'];
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = "From:" . $from;
session_start();
$_SESSION['top'] = $topic;
mail($to,$subject,$message,$headers);
?>
another php file is
<?php
session_start();
$topic = $_GET['top'];
?>
<h1><center>Meeting Invitation</center></h1>
<form action="my.php" method="post">
You are invited for the meeting on <?php echo $topic;?>
proposed dates are :<br><br>
Date Time<br>
10 jan,2015 10.00 am<input type = "radio" name = "dat" <?php if
(isset($dat) && $dat=="val1") echo "checked";?> value = "val1" checked="true" ><br>
12 feb,2015 12.15 am<input type = "radio" name = "dat" <?php if
(isset($dat) && $dat=="val2") echo "checked";?> value = "val2" ><br><br>
Proposed locations are :<br>
Location 1 : Islamabad <input type = "radio" name = "location" <?php
if (isset($location) && $location=="val1") echo "checked";?> value = "val1" checked="true" >
<br>
Location 2 : Rawalpindi <input type = "radio" name = "location" <?php if
(isset($location) && $location=="val2") echo "checked";?> value = "val2" ><br><br>
Do you want travel facility ?
<input type="checkbox" name="check_list1" value="yes"> <br><br>
Do you want hotel facility ?
<input type="checkbox" name="check_list2" value="yes"> <br><br><br>
<input type="submit" name="send" value="Send Response">
<input type="reset" >
</form>
<?php
?>

It looks like you are using $_GET instead of $_SESSION in the second file.
Change
$topic = $_GET['top'];
to
$topic = $_SESSION['top'];

Related

How to send a radio button and a checkbox value from a html form?

I'm creating a form with select, input, radio buttons and checkboxes and others.
Part of the code where is the part of my question is like this:
formulario.html
<form action="suporte_email.php" method="post" name="formulario" id="formulario" >
<div class="conjunto7" id="conjunto7">
<label for="garantia">Garantia</label><br>
<div>
<input id="radio1" type="radio" name="radio" value="Sim">
<label for="radio1"><span><span></span></span> Sim </label>
</div>
<div>
<input id="radio2" type="radio" name="radio" value="Não">
<label for="radio2"><span><span></span></span> Não </label>
</div>
</div>
<br><br><br><br>
<div class="conjunto8" id="conjunto8">
<label for="contrato">Contrato</label><br>
<div>
<input id="checkbox1" type="checkbox" name="checkbox" value="Sim">
<label for="checkbox1"><span></span> Sim </label>
</div>
<div>
<input id="checkbox2" type="checkbox" name="checkbox" value="Não">
<label for="checkbox2"><span></span> Não </label>
</div>
</div>
</form>
suporte_email.php
//Get Data
$nome = $_POST['nome'];
$empresa = $_POST['empresa'];
$contacto = $_POST['contacto'];
$email = $_POST['email'];
$marca = $_POST['marca'];
$other = $_POST['other'];
$serial_number = $_POST['serial_number'];
$garantia = $_POST['garantia'];
$contrato = $_POST['contrato'];
$permissoes = $_POST['permissoes'];
$descricao_avaria = $_POST['descricao_avaria'];
$checkbox = $_POST['checkbox'];
$radio = $_POST['radio'];
// Parse/Format/Verify Data
$to = "teste#gmail.com";
$from = '';
$subject = "Formulário de Suporte";
$email_body = "$crlf De: $nome$crlf Email: $email$crlf Assunto: $subject$crlf$crlf Empresa: $empresa$crlf Contacto: $contacto$crlf Marca: $marca$crlf Outra: $other$crlf Número de Série: $serial_number$crlf Garantia: $garantia$crlf Contrato: $contrato$crlf Tipo de Suporte: $permissoes$crlf$crlf Descrição da Avaria: $descricao_avaria";
// Setup EMAIL headers, particularly to support UTF-8
// We set the EMAIL headers here, these will be sent out with your message
// for the receiving email client to use.
$headers = 'From: ' . $from . $crlf .
'Reply-To: ' . $email . $crlf .
'Content-Type: text/plain; charset=UTF-8' . $crlf .
'Para: Website' . $to . $crlf .
'X-Mailer: PHP/' . phpversion();
// Then we pass the headers into our mail function
mail($to, $subject, $email_body, $headers);
header('Location: agradecimentos.html');
}
Basically the variables from the radio button and from the checkbox don't appear when I receive the email. I want to receive the selected value from the radio and from the checkbox.
P:S: Also tried to change where is $garantia for $radio and where is $contrato for $checkbox.
Your checkboxes have the same name:
<input id="checkbox1" type="checkbox" name="checkbox" value="Sim">
<input id="checkbox2" type="checkbox" name="checkbox" value="Não">
That means the LAST checkbox in the form is the one whose value is set in $_POST.
Duplicate names are acceptable for radio buttons, because they're an OR-type select - only one single radio button in a group can be selected. But checkboxes are an AND - you can select multiple checkboxes, which means each one has to have a unique name.
Alternatively, you can use PHP's array-name hack:
<input id="checkbox2" type="checkbox" name="checkbox[]" value="Não">
^^---
which tells PHP to expect multiple different values for the same field name, and it'll produce an array in $_POST of each of those values.

php file submit button not sending response

In my code there is an html form. I want to send response via email and display a line "your message was send" on clicking the button, but its not working ... nothing happens when I click send response button.
Please help ... thank you
<?php
if (isset($_post['send'])) {
$ToEmail = 'abc#gmail.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$check_list1 = $_POST['check_list1'];
if ($check_list1 != 'Yes') {
$check_list1 = 'No';
}
$check_list2 = $_POST['check_list2'];
if ($check_list2 != 'Yes') {
$check_list2 = 'No';
}
$MESSAGE_BODY = "Date ".$_POST["dat"]."";
$MESSAGE_BODY .= "Location ".$_POST["location"]."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<h1><center>Meeting Invitation</center></h1>
<form action="my.php" method="post">
You are invited for the meeting on company crisis
proposed dates are :<br><br>
Date Time<br>
10 jan,2015 10.00 am<input type = "radio" name = "dat" <?php if
(isset($dat) && $dat=="val1") echo "checked";?> value = "val1" checked="true" ><br>
12 feb,2015 12.15 am<input type = "radio" name = "dat" <?php if
(isset($dat) && $dat=="val2") echo "checked";?> value = "val2" ><br><br>
Proposed locations are :<br>
Location 1 : Islamabad <input type = "radio" name = "location" <?php if
(isset($location) && $location=="val1") echo "checked";?> value = "val1" checked="true" ><br>
Location 2 : Rawalpindi <input type = "radio" name = "location" <?php if
(isset($location) && $location=="val2") echo "checked";?> value = "val2" ><br><br>
Do you want travel facility ?
<input type="checkbox" name="check_list1" value="yes"> <br><br>
Do you want hotel facility ?
<input type="checkbox" name="check_list2" value="yes"> <br><br><br>
<input type="button" name="send" value="Send Response">
<input type="reset" >
</form>
<input type="button" name="send" value="Send Response">
Type needs to be submit unless you have a separate event handler in your Javascript to send the data.
<input type="submit" name="send" value="Send Response">
Otherwise no event is fired except the mouse clickies.
And yeah your PHP needs $_POST not $_post but that's not really what's affecting the send button being unresponsive.
Basic PHP 101: Variable names are case sensitive
if (isset($_post['send'])) {
^^^^^--- undefined variable
It should be $_POST. If you had display_errors and error_reporting turned on, you'd have been warned about using an undefined variable, and treating that undefined variable as an array. The debug options should never be off on a devel/debug system in the first place.
You have not closed your else condition
at the end of the file close the else tag
</form>
<?php }
And follow Marc B and Deryck's guide lines

PHP validation fail

I created Err code so that name and radio has to be checked otherwise it can't move on to the confirmation page and send error message next to the field. Please help if I'm missing any code!
<?php
$nameErr = $charityErr = "";
$name = $charity = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is missing";
}
else {
$name = $_POST["name"];
}
if (!isset($_POST["charity"])) {
$charityErr = "You must select 1 option";
}
else {
$charity = $_POST["charity"];
}
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link type="text/css" href="testStyle.css" rel="stylesheet"/>
<title>Survey</title>
</head>
<body>
<div><!--start form-->
<form method="post" action="submit.php">
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "1") echo "checked"; ?> value="1">1
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "2") echo "checked"; ?> value="2">2
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "3") echo "checked"; ?> value="3">3
<span class="error"><?php echo $charityErr;?></span>
<input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME">
<span class="error"><?php echo $nameErr;?></span>
<input type="submit" name="submit" value="Submit"/>
</form>
</div><!--end form-->
</body>
</html>
My submit.php says:
/* Subject and Email Variables */
$emailSubject = 'Survey!';
$webMaster = 'myname#email.com';
/* Gathering Data Variables */
$name = $_POST ['name'];
$charity = $_POST ['charity'];
//create a new variable called $body line break, say email and variable, don't leave any space next to EOD - new variable $body 3 arrows less than greater than, beyond EOD no spaces
$body = <<<EOD
<br><hr><br>
Company Name: $name <br>
Charity: $charity <br>
EOD;
//be able to tell who it's from
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
/* Results rendered as HTML */
$theResults = <<<EOD
<html>
blah blah
</html>
This redirects fine to submit.php page except my validation doesn't work and sends blank data.
Form code is above as:
<form method="post" action="submit.php">
<input type="radio" name="charity"
<?php if (isset($charity) && $charity == "1") echo "checked"; ?>
value="1">1
<input type="radio" name="charity"
<?php if (isset($charity) && $charity == "2") echo "checked"; ?>
value="2">2
<input type="radio" name="charity"
<?php if (isset($charity) && $charity == "3") echo "checked"; ?>
value="3">3
<span class="error"><?php echo $charityErr;?></span>
<input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME">
<span class="error"><?php echo $nameErr;?></span>
<input type="submit" name="submit" value="Submit"/>
</form>
You have a syntax error here:
<?php if (isset($charity) && charity == "3") echo "checked"; ?>
You miss $ in charity var:
<?php if (isset($charity) && $charity == "3") echo "checked"; ?>
About your second question, I think that your form is a little messy.
You can use the same page to the form, validation, error management and proccesing, using this structure:
capture vars
validating
proccesing
show errors if any or success msg
render form if error or not sent
Try some like this:
<?php
//Capture POST/GET vars
$charity = $_REQUEST['charity'];
$name = $_REQUEST['name'];
$step = $_REQUEST['step'];
//You can add some sanitizing to the vars here
//Form sent if step == 1
if ($step == 1){
/*
* Validate form
*/
//Initialize error's array
$error = array();
//No charity value error
if (!$charity){
$error[] = 'You must select 1 option';
}
//Missing name error
if (!$name){
$error[] = 'Name is missing';
}
//Add any other validation here
/*
* Process form if not error
*/
if (!$error){
//Send eMail
$subject = "Your subject here";
$toemail = "<yourname#example.com>";
$bounce = "<bounce#example.com>";
$message = "
Company Name: $name<br>
Charity: $charity <br>";
$subject = '=?UTF-8?B?'.base64_encode(utf8_encode($subject)).'?=';
$headers = "From: <webform#example.com>" . "\r\n" .
"Reply-To: <info#example.com>" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($toemail, $subject, $message, $headers, "-f $bounce" );
//Add any other proccesing here, like database writting
}
/*
* Show errors || succsess msg on the top of the form
*/
if ($error){
unset($step); //if error, show the form
echo '<div style="color:yellow;background-color:red;">ERROR:<br>';
foreach ($error as $e){
echo '- '.$e.'<br>';
}
echo '</div><br>';
}else{
echo '<div>Form succesfully sent</div><br>';
}
}
/*
* Form rendering
*/
if (!$step){
?>
<form method="post" action="">
<input type="radio" name="charity" value="1" <?php echo ($charity == "1") ? ' checked':''; ?>>1
<input type="radio" name="charity" value="2" <?php echo ($charity == "3") ? ' checked':''; ?>>2
<input type="radio" name="charity" value="3" <?php echo ($charity == "3") ? ' checked':''; ?>>3
<input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME">
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
}

what to change on php code to submit form with onClick="document.getElementById('form').submit()">SUBMIT</a>?

Two problems with a php mailer :
The 1st one
• Mailer does not work with :
<a class="button" href="javascript:" onClick="document.getElementById('form').submit()">SUBMIT</a>
• It uses to work well with :
<input type="Submit" name="Submit" value="send" class="submitbutton"/>
If I replace the button by the above input then it works.
However I have never used it with radiobuttons before : the form is submitted but radiobuttons are not processed which is trouble number 2 in this post;
The script seems to be created for multi alternative checkboxes and not for selective radiobutton.
What has to be changed to accomodate both submit button and radiobuttons ?
FORM
<form class="form-1" id="form" method="POST" action="php/subscription.php">
<input type="text" id="name">
<input type="text" id="phone">
<input type="text" id="email">
<input type="checkbox" name="check[ ]" value="iwhishtoreceivemailing" > WITH CHECKBOX VALUES ARE SUBMITTED
<input type="radio" name="check[ ]" id=""> ????????????? don´t know how to change the code for radiobuttons
<input type="text" id="message">
<a class="button" href="javascript:" onClick="document.getElementById('form').submit()">SUBMIT</a>
how to make it work with this php script ??
MAILER.PHP (php code is pasted below)
<?php
if(isset($_POST['Submit'])) {
$to = "anything#whatever.com ";
$to2 = "johndoe#jackfrost.com";
$subject = "contact_form";
$name = $_POST['visitor_name'];
$phone = $_POST['one'];
$email = $_POST['email'];
$message = $_POST['message'];
foreach( (array)$_POST['check'] as $value){
$check_msg .= "Checked: $value\n";
}
$body = "Name: $name\n phone: $phone\n Email : $email\n Mailing: $iwhishtoreceivemailing\n Message: $message\n";
header("Location: thankyoupage.html");
mail($to, $subject, $body );
mail($to2, $subject, $body);
} else {
echo "error_invalid_function";
instead of :
if(isset($_POST['Submit'])) {
do :
if(isset($_POST['email'])) {
because submit button that have name Submitdoes not exsite any more

PHP Array in email

Im trying to write a contact form and so far everything works only theres a multiple checkbox in the form and im unsure how to call all data and so my email returns 'array' for the variable 'service'
My code is...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$service = $_POST['service'];
$description = $_POST['description'];
$location = $_POST['location'];
$to = "liam#.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Number: $number,
Service: $service,
Description: $description
Location: $location
";
//end of message
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: $email\r\n";
mail($to, $subject, $message, $headers);
?>
My form for the service input is...
<dt><input type="checkbox" value="Static guarding" name="service[]" class="cbox">Static guarding</dt>
<dt><input type="checkbox" value="Mobile Patrols" name="service[]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[]"class="cbox">Alarm response escorting</dt>
<dt><input type="checkbox" value="Alarm response/ Keyholding" name="service[]"class="cbox">Alarm response/ Keyholding</dt>
<dt><input type="checkbox" value="Other" name="service[]"class="cbox">Other</dt>
<dt><input type="hidden" value="Other" name="service[]"class="cbox"></dt>
You can process the checkbox values into a string with implode():
$checkboxes = implode(',', $_POST['service']);
$message = <<<EOL
...
Service: $checkboxes
EOL;
Note the use of a heredoc - it's a MUCH nicer way of defining a multi-line string than using a regular quoted string.
Because you appended your form input names with brackets, PHP will form an indexed array out of them.
$sCount = count($service);
for ($s=0; $s<$sCount; $s++) {
echo $service[$s];
}
You might be better off creating an associative array:
<dt><input type="checkbox" value="Mobile Patrols" name="service[mobile]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[escorting]"class="cbox">Alarm response escorting</dt>
That way, in your script, you could access them as:
echo $service['mobile'];
echo $service['escorting'];
Note that empty checkboxes aren't submitted so you'll want to first check if the array element is set.

Categories