PHP Header redirect with Radio button - php

I've got this code from the web. It functions with my setup, calling the command all ok, i have the variables entered correctly.
But the if .. else statement redirects to only one URL, not the if statement, but only the else. so no matter what radio i select the https://thisdomain.com/1-2/ domain loads.
Here is the PHP/Html code:
<?php
$radio832112001 = intval($_POST['radio832112001']);
if ($radio832112001 === 2)
{
header("Location: https://thisdomain.com/2-2/");
die;
}
else
{
header("Location: https://thisdomain.com/1-2/");
die;
}
?>
<form action="https://thisdomain.com/code/action-900.php">
<input type="radio" id="no" name="radio832112001" value="2">
<label for="indoor-220">No</label>
<input type="radio" id="yes" name="radio832112001" value="1">
<label for="outdoor-220">Yes</label>
<input type="submit" value="Submit">
</form>
What am i missing?
Cheers in advance.
Steve

$radio832112001 = intval($_POST['radio832112001']); with:
$radio832112001 = intval($_GET['radio832112001']); or
add method="POST" to your form tag.
This worked. With updating the for="" code too. It was wrong. Now it all works as intended.
Thanks all

You are using GET method in your form by not declaring method keyword. Replace
$radio832112001 = intval($_POST['radio832112001']);
by
$radio832112001 = intval($_GET['radio832112001']);
or add method="POST" to your form tag.

Related

Can't use a form's value in a different php file

Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.

Send form data to the same page not functionning

I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.

Displaying the form if the post is empty or result if it has already been submitted

This is my first post and I'm complete beginner so please be gentle :)
I'm trying to create a form that after submitting an account name would check and return a CNAME of the host (account+domain.com)
The problem is that I want to do it all on the same website so it will either display the form if nothing has been posted or display the result otherwise.
This is what I've created, it seems that I'm not calling the POST correctly, but I can't really get what am I doing wrong.
Please help
<?php
if(isset($_POST[DomainSubmit])){
$AccountName = $_POST[ClientDomain];
$CName = dns_get_record($AccountName."domain.com", DNS_CNAME);
echo '<h1>'.$CName.'<h1>';
}
echo'<form action="index.php" method="POST" ">
<input type="text" name="ClientDomain">
<input type="submit" name="DomainSubmit">
</form>'
?>
Try to add else so it will be displayed one or another stuff
<?php
if(isset($_POST['DomainSubmit']) && isset($_POST['ClientDomain'])){
$AccountName = $_POST['ClientDomain'];
$CName = dns_get_record($AccountName."domain.com", DNS_CNAME);
echo '<h1>'.$CName.'<h1>';
} else {
echo'<form action="index.php" method="POST" ">
<input type="text" name="ClientDomain">
<input type="submit" name="DomainSubmit">
</form>'
}
?>
Edit:
You forgot to properly write array POST (missing quotes)
$_POST[DomainSubmit]
And it should be
$_POST['DomainSubmit']

Add Checkbox value to salesforce

im new to salesforce integration.
i have a contact form in which i have a field called Optin/Optout filed which is checkbox. if the user tick that checkbox value should get captured in salesforce. but i could not do it. can anyone please help me in this.
here is my code what i have done so far.
index.php
<form action="salesforce.php">
<input type="checkbox" name="news" value="1" checked="checked"/>
</form>
salesforce.php
<?php
$newsletter = $_POST['news'];
if($newsletter == "1"){
$news_result = $newsletter;
}else{
$news_result = "0";
}
$req .= "&HasOptedOutOfEmail=" . urlencode($news_result);
?>
You miss the method in the form, if no method is defined it will be a get method and you use post the get the value
if you do this it will work:
<form action="salesforce.php" method="post">
<input type="checkbox" name="emailOptOut" id="emailOptOut" value="1" />
this solved my problem
Try using this it worked for me:
apex:inputCheckbox value="{!news.isSelected}"
selected="{!news.isSelected}" immediate="true"

$_GET is not working with radio buttons

Does anyone know why this code will not work? I simply want to print which radio button is selected. It always prints 'Null' no matter what is selected. PHP code is below.
<?php
$conn = mysql_connect('localhost','student','student') or die(mysql_error());
mysql_select_db('vgs',$conn);
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
//Process Question 1
echo "".$q1option;
The HTML code is below.
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
Any help with this would be greatly appreciated. Note I have many tables in the 'vgs' database, if that makes a difference.
Thank you,
Daniel
Additional Code
HTML below
<input type="button" value="Submit" onclick="result();" />
<input type="reset" value="Reset" />
</form>
Embedded JavaScript below. It uses http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
function result()
{
$('#Suggestion').load('process_answers.php');
}
Looks like your submit button is outside of the <form> tag, it should be within the form:
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
<input type="submit" value="Submit" />
</form>
You are always getting Null in return because your query string is not being attached anywhere. If you are using AJAX, you need to pass the query string data. Your cycle is not working properly because of a misconfiguration.
Take the following code:
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
Using $_GET will attempt to extract from your query string a variable labeled 'q1option'. If this variable is not set, then return 'Null'. So your code is working as expected as it is currently structured.
If you want to pass the variable between pages you will either need to submit the form and the variable will be automatically passed, or you need to modify your jQuery to allow for the passing and processing behind the scenes of your variable. Once processed, you would then handle the results accordingly and render them to the client as desired.

Categories