I have a php code as shown below in which I am getting error Undefined offset: 0 at Line A and Undefined offset: 1 at Line B
<?php
$area = &$_SESSION['diagram']->areas[$index];
// If a new one, create it and link in the session
if (!is_object($area)) {
$area = new DiagramArea();
$_SESSION['diagram']->areas[$index] = &$area;
}
for ($i = 0; 5 > $i; ++$i) {
var_dump($area->files); // Line M
?>
<fieldset>
<legend>Name / Pattern</legend>
<input type="text" name="label<?php echo $i; ?>"
value="<?php echo is_object($area->files[$i]) ? $area->files[$i]->label : ''; ?>" /> // Line A
<input type="text" name="pattern<?php echo $i; ?>"
value="<?php echo is_object($area->files[$i]) ? $area->files[$i]->pattern : ''; ?>" /> // Line B
</fieldset>
<?php } ?>
For debugging purpose, I have added Line M in the code above. At Line M, I am getting the following o/p:
./abc.php:99:
array (size=0)
empty
Problem Statement:
I am wondering what changes I need to make at Line A and Line B so that I can avoid the warning in both of those lines.
Because you are performing an is_object test, it might be worth it to make sure your object matches what you expect by performing a property_exists test, too. However, it might be overkill for this, too (although it doesn't hurt).
This version breaks things on to multiple lines which I personally believe makes it more readable. It also uses the alternative control structure for PHP which doesn't use curl braces. This is optional, but, once I again, I personally find it easier to read when switching between HTML and PHP so that I don't need to chase curly braces around. Lastly, I try very hard to not perform a lot of logic within an HTML attribute, and instead just echo. The reason for this is that when debugging, the HTML parsing algorithm might hide PHP errors in the browser.
<?php for ($i = 0; 5 > $i; ++$i) : ?>
<fieldset>
<legend>Name / Pattern</legend>
<input
type="text"
name="label<?php echo $i; ?>"
<?php if (isset($area->files[$i]) && is_object($area->files[$i]) && property_exists($area->files[$i], 'label')): ?>
value="<?php echo $area->files[$i]->label; ?>"
<?php endif; ?>
/>
<input
type="text"
name="pattern<?php echo $i; ?>"
<?php if (isset($area->files[$i]) && is_object($area->files[$i]) && property_exists($area->files[$i], 'pattern')): ?>
value="<?php echo $area->files[$i]->pattern; ?>"
<?php endif; ?>
/>
</fieldset>
<?php endfor; ?>
Make sure that if you use this that you don't accidentally miss the closing /> on the input elements.
Related
I'm a beginner at coding, and I have to make a small game in php for school.
I have to use a hidden input, like this:
<form action="process.php" method="post">
<input type="hidden" name="rand" value="<?php rand(1,10); ?>" />
</form>
The value stands for a random number between 1 and 10 (I hope the value is correct). Now, in process.php I want to retrieve the random number by using post, so what I tried to do is the following:
<?php $random = $_POST['rand'];
echo $random; ?>
In my browser (Firefox), I'm getting the following error:
Notice: Undefined index: rand in
G:\xampp\htdocs\process.php on line 2
Does anyone know how I can echo the hidden value without using complex techniques?
Thanks in advance,
Maxime
You haven't echoed your rand function within the hidden input tag.
Also, there should be a submit button. Only then you can access the POST parameters.
<input type="hidden" name="rand" value="<?php echo rand(1,10); ?>" />
<input type="submit" name="submit" value="submit">
Try something like this:
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_POST); // See your POST array
$random = $_POST['rand'];
echo $random;
}
Hope this helps.
Peace! xD
I'm using the server to write the html of a lot of my page to process some pagination. I'm getting a syntax error on the line just below the comment insert as shown below. I'm guessing it is because I'm trying to place echo within a echo? I've tried a lot of things here and am having no luck figuring out what I'm doing wrong. Any help appreciated greatly!
<?php
PDO STUFF HERE. . .
if() {
foreach() {
echo ' <input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
//following line showing syntax error in my text editor
<input name="transaction" type="radio" tabindex="11" value="0"' . if($transaction == '0') echo '$chkvalue'; . '/><label for="listings">Listings</label>
//following line showing syntax error in my text editor
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' . if($transaction == '1') echo $chkvalue; . '/> '
}
}
?>
if and echo are language constructs and as such cannot be used inside a string. You'll have to change the flow of the logic to fix this.
Easiest fix is to just use a bunch of semicolons.
This is not allowed:
echo 'abc' . if( $a == 1 ) { echo '3'; };
But this is fine, because it breaks everything up into steps:
echo 'abc';
if( $a == 1 ) { echo '3'; }
Try something like this:
echo '<input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
<input name="transaction" type="radio" tabindex="11" value="0"'.($transaction == '0') ? $chkvalue:"".'/><label for="listings">Listings</label>
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' .($transaction == '1') ? $chkvalue:"".'/>';
Using the ? operator, you can check a condition and echo something out if it equates, or nothing if it doesn't.
You cannot concatinate an entire if statement. It cannot be converted to a string.
You can change it to an expression with a ternary operator though:
echo 'first part of your string ' . ($transaction == '0'?'$chkvalue':'') . ' rest of your string';
Even better (I think), you can put the HTML outside of PHP tags. You can briefly open PHP tags again to execute and if statement to conditionally echo a value, or you can use the short <?= XYZ ?> tag which is short for <?php echo XYZ ?>.
This way of working is very convenient when working with larger chunks of HTML, because most syntax highlighters will actually highlight the HTML, and you won't need to escape anything. If you 'prepare' your variables before and insert them using the short <?= tags, your HTML will look like a clean template-like piece of code with only those variables in it.
<?php
foreach( ... )
{?>
<input name="flyer" type="file" id="<?=$row['ad_link']?>" tabindex="9" /></td>
<input name="transaction" type="radio" tabindex="11"
value="0" <? if($transaction == '0') echo $chkvalue; /* Actual statement */ ?>/>
<label for="listings">Listings</label>
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12"
value="1" <?= ($transaction == '1'? $chkvalue : '') /* Just output an expression */ ?>/>
<?}
I'm kinda rookie at PHP but i'm trying to developing a backoffice with some tables and value-editing options.
By now, i have tables like this:
But i'm kinda bugged with this issue:
The change button is being implemented like this:
for ($i = 0; $i < count($a); $i++) {
?>
<tr>
<td><i onclick="
document.form1.deviceFeatValID.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE_ID] ?>;
document.form1.deviceID.value = <?php echo $a[$i][DEVICE_ID] ?>;
document.form1.deviceClassFeatID.value = <?php echo $a[$i][DEVICE_CLASS_FEATURE_ID] ?>;
document.form1.deviceFeatureVal.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE] ?>;
document.form1.submit();" class="icon-refresh" ></td>
<td><?php echo $a[$i]['DEVICE_FEATURE_VALUE_ID']; ?><td><?php echo $a[$i]['DEVICE_ID']; ?><td><?php echo $a[$i]['DEVICE_CLASS_FEATURE_ID']; ?><td><?php echo $a[$i]['DEVICE_FEATURE_VALUE']; ?>
</tr>
<?php
}
?>
That is, by Javascript, i set the input values of this form, in the same .php:
<form name="form1" method="post" action="deviceFeatureValueFRM.php">
<input type="hidden" name="deviceFeatValID"/>
<input type="hidden" name="deviceID"/>
<input type="hidden" name="deviceClassFeatID"/>
<input type="hidden" name="deviceFeatureVal"/>
<input type="hidden" name="hiddenTypeField"/>
</form>
But...do you consider that this is a good practice? Is there another solution?!
The goal of this code is to detect which line is going about to be edited.
Kind regards,
Sam
Why do this with javascript? Just give the input field a value as such:
<input type="hidden" name="deviceFeatValID" value="<?php echo $a; ?>"/>
Where $a can be any variable you would like.
Well, I would recommend that you had all files separately. So let's say you had one file for PHP. One file for Javascript, and one for HTML. So if you want to use php functions or whatever just need to call them via AJAX.
That way you will have a cleaner code and a bit more secure one.
hi have a problems with magento as it shows me this error when i click sunglasses menu:
Notice: Use of undefined constant id - assumed 'id' in /var/www/app/design/frontend/glassesonline/default/template/mana/filters/items/list.phtml on line 159
This is code at line 159
<input id="category" type="hidden" value="<?php $arr = $this->getRequest()->getParams(id,false); echo $arr['id']; ?>">
This drive me crazy, please let me know where am i wrong, i will provide more info if needed.Thanks
it should be:
<input id="category" type="hidden" value="<?php $arr = $this->getRequest()->getParams('id',false); echo $arr['id']; ?>">
^^^
you are missing quotes here and PHP is thinking you are trying to use a defined varuiable which you do not have. Always use quotes for strings.
I'm new in PHP and I'm getting this error:
Notice: Undefined index: productid in /var/www/test/modifyform.php on
line 32
Notice: Undefined index: name in /var/www/test/modifyform.php on line
33
Notice: Undefined index: price in /var/www/test/modifyform.php on line
34
Notice: Undefined index: description in /var/www/test/modifyform.php
on line 35
I couldn't find any solution online, so maybe someone can help me.
Here is the code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="rowID" value="<?php echo $rowID;?>">
<p>
Product ID:<br />
<input type="text" name="productid" size="8" maxlength="8" value="<?php echo $productid;?>" />
</p>
<p>
Name:<br />
<input type="text" name="name" size="25" maxlength="25" value="<?php echo $name;?>" />
</p>
<p>
Price:<br />
<input type="text" name="price" size="6" maxlength="6" value="<?php echo $price;?>" />
</p>
<p>
Description:<br />
<textarea name="description" rows="5" cols="30">
<?php echo $description;?></textarea>
</p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</form>
<?php
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
What I do after that (or at least I'm trying) is to update a table in MySQL.
I really can't understand why $rowID is defined while the other variables aren't.
Thank you for taking your time to answer me.
Cheers!
Try:
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['price'])) {
$price = $_POST['price'];
}
if (isset($_POST['description'])) {
$description = $_POST['description'];
}
?>
Apparently the index 'productid' is missing from your html form.
Inspect your html inputs first. eg <input type="text" name="productid" value="">
But this will handle the current error PHP is raising.
$rowID = isset($_POST['rowID']) ? $_POST['rowID'] : '';
$productid = isset($_POST['productid']) ? $_POST['productid'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$price = isset($_POST['price']) ? $_POST['price'] : '';
$description = isset($_POST['description']) ? $_POST['description'] : '';
This is happening because your PHP code is getting executed before the form gets posted.
To avoid this wrap your PHP code in following if statement and it will handle the rest no need to set if statements for each variables
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))
{
//process PHP Code
}
else
{
//do nothing
}
TRY
<?php
$rowID=$productid=$name=$price=$description="";
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
There should be the problem, when you generate the <form>. I bet the variables $name, $price are NULL or empty string when you echo them into the value of the <input> field. Empty input fields are not sent by the browser, so $_POST will not have their keys.
Anyway, you can check that with isset().
Test variables with the following:
if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL
You better set it to NULL, because
NULL value represents a variable with no value.
Hey this is happening because u r trying to display value before assignnig it
U just fill in the values and submit form it will display correct output
Or u can write ur php code below form tags
It ll run without any errors
If you are using wamp server , then i recommend you to use xampp server .
you . i get this error in less than i minute but i resolved this by using (isset) function . and i get no error .
and after that i remove (isset) function and i don,t see any error.
by the way i am using xampp server
this error occurred sometime method attribute ( valid passing method )
Error option :
method="get" but called by $Fname = $_POST["name"];
or
method="post" but called by $Fname = $_GET["name"];
More info visit http://www.doordie.co.in/index.php
To remove this error, in your html form you should do the following in enctype:
<form enctype="multipart/form-data">
The following down is the cause of that error i.e if you start with form-data in enctype, so you should start with multipart:
<form enctype="form-data/multipart">