I'm new to PHP and i've been learning PhP like a week ago so please bear with me.
While i was trying to echo out an else statement if any variable in the POST request is different or not set.
<form action="process.php" method="POST">
<p> <input type="radio" name="language" value="PHP"> PHP <br>
<input type="radio" name="language" value="RUBY"> RUBY <br>
<input type="radio" name="language" value="HTML"> HTML <br>
</p>
<input type="Submit" value="Submit">
</form>
process.php
<?php
$lang = $_POST['language'];
if (isset($lang)) {
echo $lang;
}
else {
echo 'Buzz off';
}
?>
While its showing error like :
Notice: Undefined index: language in
/Applications/XAMPP/xamppfiles/htdocs/process.php on line 2
Please help :)
You're trying to get an undefined array index and put it into a variable, and this will throw an exception.
You should check if the array key isset before you try to put it in a variable, like this:
if ($_POST['language']) {
$lang = $_POST['language'];
echo $lang;
}
else {
echo 'Buzz off';
}
If you are using PHP7, you can use the Null coalescing operator.
$lang = $_POST['language'] ?? 'Buzz off';
echo $lang;
Related
I have a form which submits data, and as a test I am attempting to first check if a variable is set on submit. If the variable is set, a text will be displayed stating something like "Variable is set". However if it isn't set, a text will be displayed saying "variable not set", once the variable hasn't been set it should then be set so next time when the form is submitted it displays variable is set however this is not working for me, for some reason it always displays that the variable is not set, my PHP code will be below:
<?php
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
I feel really stupid for not being able to do something which seems so easy, I am only just learning and sort of trying to teach myself, thank you for any help provided!!!
Working code and explanation:
<?php
$test="";
if (isset($_POST["text"])) { //always directly check $_POST,$_GET var without assigning
echo "This var is set";
$test=$_POST["text"]; // then assign
}
else{ // and use else clause for otherwise case
echo "This var is not set";
$test = 'set'; // AND if you want set to default/custom value in case of not set.
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off">
<br /><br />
<input type="submit" value="submit">
</form>
If you are using form to submit values, then try this one,
if (isset($_POST['text'])) {
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
Otherwise, If a variable set to empty value like $test = '';
(It means variable is set but it has no values) It will execute your first if condition only.
<?php
$test=$_GET["text"];
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="#" method="get">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
You haven't declared the variable $test.
Unless you've still got a bit of PHP somewhere that you haven't included here, your variable is empty. When a form is submitted, the input will be added to either the $_POST array (for method = "post") or else the $_GET array (for method = "get").
To Fix:
<?php
if (isset($_POST['text'])) {
$test = $_POST['text'];
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
I am using forms select. I just want to check what user selects by echo-ing the result on the same page so I kept the action="". But its showing error undefined index slct. Can any one please help me
<form action="" method="post">
<select name="slct">
<option value="yes" selected="selected"> yes </option>
<option value="no"> no </option>
</select>
<input type="button" value="Submit" />
</form>
<?php
$tofd = $_POST["slct"];
echo $tofd;
?>
Why its showing the error
Notice: Undefined index: slct in C:\wamp\www\Univ Assignment\Untitled-4.php on line 21
You should use button type submit NOT button
<input type="submit" value="submit" />
And then test IT like
echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..';
Use PHP isset to check if its exist first
Example :
$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;
Example 2 Using a function
function __POST($var)
{
return isset($_POST[$var]) ? $_POST[$var] : null ;
}
$tofd = __POST("slct");
If they are on the same page, initaially, $_POST would be empty because the user has not posted anything. So you have to handle that.
if(isset($_POST["slct"]))
$tofd = $_POST["slct"];
<?php
if (isset($_POST["slct"])){
$tofd = $_POST["slct"];
echo $tofd; }
?>
I am new to PHP and I was making this form and I wanted to print some data but it is not displaying. What is wrong with it? Here's the code:
<form name="input" action="check.php" method="get">
Unit number:
<input type="number" name="unit" />
<input type="submit" value="Submit" />
</form>
<table>
<tr><td class="check-table">
<?php
if($_GET[unit] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
</td></tr></table>
Please Help?
The better way would be:
if (empty($_GET['unit'])) {
$output="<p>Please Enter A Unit Number</p>";
echo $output;
}
The reasons:
You check if variable exists
You use ' quotes for array key name
You output $output variable only if it is necessary. And in your case - you output it even if it doesn't exist
You've also confused == (comparison operator) and = (assignment operator)
I think you missed the single quotes in the $_GET['unit']
<?php
if($_GET['unit'] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
I am using Zend framework. In this i create a model and put my database connection in this model.
Here is my code so far :-
public function getTagusers(){
try {
$stat = $this->db->query("select a.tagCode child, b.tagCode parent " .
"from tag a, tag b where a.tagParentId=b.tagId");
$aResultData = $stat->fetchall();
}
catch(Exception $e){
error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '
. $e->getMessage());
}
return $aResultData;
}
Now I am using action in controller. My code is so far :-
public function listAction()
{
$tagusers =new Admin_Model_DbTable_Tagusers();
$this->view->taguser =$tagusers->fetchall();
}
Now finally i want to echo my data in view list.html. My code is so far :-
<script>
<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
} else {
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}
// End -->
</script>
<?php foreach($this->taguser as $taguser) ?>
<form name="myform" action="checkboxes.asp" method="post">
<b>Select Allowed keywords below:</b><br>
<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
<br>
<input type="checkbox" name="check_list" value="1">
<?php echo $this->escape($taguser->tagCode);?><br>
<input type="checkbox" name="check_list" value="2">
<?php echo $this->escape($taguser->tagParentId);?><br>
</form>
But I am not able to echo the data properly. Can anyone explain me what I can do to echo the result according to my query.
first if you using Zend Framework as a framework as it appears... your first error is it normal for viewscripts to have the .phtml extension (i know you may have changed this).
next your php is incorrect:
<?php foreach($this->taguser as $taguser): //need to colon for alternate loop syntax ?>
<form name="myform" action="checkboxes.asp" method="post">
<b>Select Allowed keywords below:</b><br>
<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
<br>
<input type="checkbox" name="check_list" value="1">
<?php echo $this->escape($taguser->tagCode);
//if this causes errors use
//array notation $taguser['tagCode']?><br>
<input type="checkbox" name="check_list" value="2">
<?php echo $this->escape($taguser->tagParentId);?><br>
</form>
<?php endforeach //need to end the foreach statement alternate syntax?>
I'm not going to critique your form, if you want built a separate form for each record that's your business.
Well, you're printing N forms (where N = count($this->taguser)), each one containing 3 checkboxes with the same value ('yes', '1' and '2', respectively), which makes no sense at all.
If I'm correct, your form should look like this:
<form name="myform" action="checkboxes.asp" method="post">
<b>Select Allowed keywords below:</b><br>
<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
<br>
<?php foreach($this->taguser as $taguser): ?>
<input type="checkbox" name="check_list" value="<?php echo $this->escape($taguser->tagCode);?>">
<br>
<input type="checkbox" name="check_list" value="<?php echo $this->escape($taguser->tagParentId);?>">
<br>
<?php endforeach; ?>
Still, you should read about Zend_Form. It will be hard to understand it in the beginning, but it's totally worth it.
I am creating a new website. For that website I will need to transfer a quantity and amount value from one if condition statement into another if condition statement. Both if statements are accessed by separate submit buttons named "checkamt" & "buy".
I need to transfer the "quantity", "value", and "net" values to and from the checkamt if statement to the buy if statement.
Here's my code:
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
I think that the problem you're having is that variables don't persist on page change. If you want that, you'll need to use a session. First, you must call session_start before anything, including HTML, is sent to the user. Then, you can use the $_SESSION variable.
<?php
session_start();
?>
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$_SESSION['qun']=1;
$_SESSION['val']=5000;
$_SESSION['total']=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $_SESSION['qun'];
echo $_SESSION['val'];
echo $_SESSION['total'];
}
?>
Improve your English! Not sure if this is what you want, but if you want to share the values of your variables between the two ifs? You have to declare them at a higher scope than your if:
<?php
$qun = 0;
$val = 0;
$total = 0;
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
Not sure if I'm getting the question right, but why not do something like this :
if(isset($_POST[checkamt]) || isset($_POST[buy]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
echo $qun;
echo $val;
echo $total;
}
You need to track your variables between the different forms. You can use SESSION like Xeon06 suggested, or do the following. I'm only showing for $qun:
<?php
if(isset($_POST['checkamt'])) {
$qun=1;
}
if(isset($_POST['buy'])) {
echo $qun;
}
?>
<form action="index.php" method="post">
<input type="hidden" name="qun" value="<?php echo $qun; ?>" />
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>