Radio Button with PHP - php

I wanted to know why I am not able to fetch the value of radio button into the PHP Variable. Thanks in advance!
PHP CODE:
$Type = (isset($_GET['type'])?$_GET['type'] : null);
echo "<b>Type</b>";
echo $Type;
Here no value in the type variable is getting displayed.
HTML CODE:
<input type = 'radio' name='type' value='Residential' checked<?PHP print $Type;>> Residential<br>
<input type = "radio" name="type" value="Commercial" <?PHP print $Type;?>> Commercial<br>

First, you need a form with a method (whether you want to use post or get) and an action, which will be the URL where to go to, in this case it's the same page (<?php echo $_SERVER['PHP_SELF']; ?>)
<?php
$Type = (isset($_GET['type'])?$_GET['type'] : null);
echo "<b>Type</b>";
echo $Type;
?>
<form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type='radio' name='type' value='Residential' checked<?php echo $Type;?>> Residential<br>
<input type="radio" name="type" value="Commercial" <?php echo $Type;?>> Commercial<br>
<input type="submit" value="submit" />
</form>
And you forgot the submit button to submit the form? Or you didn't, but just posted incomplete code. Anyway, above code should work.

<?php
print_r($_GET);//print values from form
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type='radio' name='type' value='Residential'> Residential<br>
<input type="radio" name="type" value="Commercial"> Commercial<br>
<input type="submit" value="submit" />
</form>

Related

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

how to send checkbox value on next page

how to send check box value in to next page without submit button and without action.
here i am going to next page using link so i want that these checkbox value send on next.php
my form is
<form method="post" action="" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
</form>
and here a link by using i am going to next page
go here
To post data from one page to another page you have to use a submit button like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
<input type="submit" value="Go here" />
</form>
also specify receiving page in action attribute of <form>
In another page (next.php).
You have to access your posted values using $_POST['name'];
eg.
echo $_POST['pay_type'];
EDIT
In your code, I can see that you want to send multiple values for checkbox pay_type, for that you have to write it as an array (name="pay_type[]")in HTML to send multiple values.
So, the complete code will looks like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type[]" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" > Monthly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);"> Yearly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);"> Lifetime
<br/>
<input type="submit" value="Go here" />
</form>
Code in next.php:
<?php
echo "<pre>";
print_r($_POST['pay_type']); //it will print complete array of values for pay_type checkbox
echo "</pre>";
?>
Log1c ツ as said before you need a button.. :). What is also possible to use an attribute onClick:
Add an ID at your form tag (id="myForm") (dont forget the target page next.php in action)
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;" id="myForm">
and then the onClick attribute
go here
jsfilleDemo

Having issue with my html form in a php page

I have tried to put this code in my webpage but I have this error :
Parse error: syntax error, unexpected '?' in E:\MyServer\htdocs\...(line 1 from this script)
CODE :
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
this code is in a .php page
Thanks.
Try to format it properly:
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
How to get the value you pass through your form:
Welcome <?php echo $_POST["name"]; ?><br>.
EDIT
Also you should add this in delete.php file:
if(isset($_POST['name'])){ $name = $_POST['name']; }
See : http://www.w3schools.com/php/php_forms.asp
You need to drop the double quotes around the variable name and move them to the actual element property.
<form action='delete.php?name="<?php echo $contact['name']; ?>"'
^ ^
It then becomes
<form action="delete.php?name=<?php echo $contact['name']; ?>"
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
This should work ..
Do you need to send the data via GET and POST to delete.php?
<form action='delete.php' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
With this you can then just do $_POST['name'] to get your contact name...
Sorry if I have missed something but cannot see why you would need it passed twice

import html value in a php function

I have this code that fetches information in a file and shows the result. That all works as expected. What I'm not able to do is to export the button value in the function when it is pressed so the value of the pressed button will transfer to a variable in my php function. How can I do that?
Here the code:
<h2>Demmarage script torrent</h2>
<form action="search.php" method="POST">
<input type="text" name="input_value">
<input type="submit" name="submit">
<?php
echo "<br>";
if (isset($_POST['submit'])){
$findme = $_POST['input_value'];
$findme1 = str_replace (" ", ".", $findme);
$savedarr = unserialize(file_get_contents('torrent.bin'));
foreach ($savedarr as $val1){
$mystring = $val1['title'];
if((stripos($mystring, $findme) !== false) or (stripos($mystring, $findme1) !== false)) {
echo "Show trouve: ";
echo $mystring;
?>
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
<?php
echo "<br>";
}
}
}
if (isset($_POST['editId'])){
//Here i want to import the value of the pressed button to do something
echo "download start";
}
?>
you should use a hidden field, like
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
After the submit you will be able to get the value with
$_POST['link']
Also, this should be in a form,otherwise the submit will post EVERY field ...
like
foreach(...)
{?>
<form action="" method="POST">
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
<input type="submit" />
</form>
<?php}
The problem is that you are setting the value twice by <button>value1</button> and by <button value="value2">... remember that the value of a submit button is always the text that shows up inside the button.
You should do replace this:
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
with this:
<form method="POST">
<input type="hidden" value="<?=$val1['link']?>" name="editId" />
<input type="submit" value="Telecharger" />
</form>

why php input blank value or duplicate values in one form if submit another form

I am encountering a strange phenomena: When I submit a form in my code, then beside doing what is there in that form, it looks like it also trigger the comment form. So when the page is reloading, I get some blank comments or duplicate comments. In my code I have several forms with submit, and one of that is the form for input comment:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id'] ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d">
</textarea>
<input type="submit" />
</form>
Could any of you point out for me where it gets wrong?
As requested, I post the whole (updated) code here:
<h3>Comments</h3>
<!-- <p>Put your comments here: </p> -->
<?php
//a: commenters
$i = addslashes($_POST['a']);
$ip = addslashes($_POST['b']);
$a = addslashes($_POST['c']);
$b = addslashes($_POST['d']);
if(isset($_POST['form1'])){
if(isset($i) & isset($ip) & isset($a) & isset($b))
{
$connector= new DbConnector();
$r = mysql_query("SELECT COUNT(*) FROM `databasename`.`ban` WHERE ip=$ip"); //Check if banned
$r = mysql_fetch_array($r);
if(!$r[0]) //Phew, not banned
{ // echo "a: ".$a." b: ".$b." ip: ".$ip." i: ".$i."";
$Date4=date('Y-m-d H:i:s');
if(mysql_query("INSERT INTO `databasename`.`Comments` VALUES ('$a', '$b', '$ip', '$Date4')"))
{
?>
<script type="text/javascript">
window.location="/index.php?id=".<?php echo $i; ?>;
</script>
<?php
}
else echo "Error, in mysql query";
}
else echo "Error, You are banned.";
}
}
$x = mysql_query("SELECT * FROM `databasename`.`Comments` ORDER BY i DESC ");
while($r = mysql_fetch_object($x)) echo "<div class='c'>".$r->a."<p>".$r->b."</p> </div>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id'] ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d">
</textarea>
<input type="submit" name="form1" />
</form>
You need use isset()
in HTML submit button you need to use name attribute for each and every form,
<input type="submit" name="form1" />
IN PHP,
<?php
if(isset($_POST['form1']){
echo $_POST['a'];
}
?>
Your code,
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id']; ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d"></textarea>
<input type="submit" name="form1" />
</form>
I would use:
if(!empty($_POST['form1']){
echo $_POST['a'];
}
empty() will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.
Read more: http://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

Categories