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.
Related
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.
Currently I have the following form:
<form id="new_account_form" action="php/new-account.php" method="post">
<span>name</span>
</br>
<input type="text" name="main_name" required></input>
<br><br>
<span>email adres</span>
</br>
<input type="text" name="main_email" required value="<?php echo $email; ?>" disabled></input>
<br><br>
<span>user</span>
</br>
<input type="text" name="main_username" required value="<?php echo $username; ?>" disabled></input>
<br><br>
</form>
As you can see both the email input field and the username input have PHP values in them that are echoed. The input field aren't empty.
The problem I am facing right now is when I submit the form and try to $_POST the input fields in the other page I keep getting the following error:
Notice: Undefined index: main_email
Notice: Undefined index: main_username
Am I echoing the variables in the wrong place or something?
The new-account.php file contains the following code:
<?php
//get data from form
$main_name = $_POST['main_name'];
$main_email = $_POST['main_email'];
$main_username = $_POST['main_username'];
echo $main_name;
echo "</br>";
echo $main_email;
echo "</br>";
echo $main_username;
echo "</br>";
?>
after a little googling, disabled forms don't post to the action page. replace disabled with readonly and you should be fine.
Replace:
<input type="text" name="main_email" required value="<?php echo $email; ?>" disabled></input>
with:
<input type="text" name="main_email" required value="<?php echo $email; ?>" readonly></input>
The disabled field values of forms are not posted.So always use readonly where you want to post the value as well as want that the value remains unchanged.
Original answer:
The reason why you're getting those notices, is because the variables have not been set in the inputs' values of your form.
I was 50% right and 50% wrong.
(Consult my edit below)
Use a ternary operator
Change: value="<?php echo $email; ?>"
to
value="<?php $email=!empty($_POST['main_email']) ? $_POST['main_email'] : ''; ?>"
or
value="<?php echo !empty($_POST['main_email']) ? $_POST['main_email'] : ''; ?>"
and do the same for the other input.
Use a conditional !empty() for the other inputs in the other file also.
Sidenote: </input> is an invalid closing tag. </br> is also invalid, it should read as <br/>
Edit: - which is now a supplemental answer:
Upon seeing the other answer about disabled I agree on that point and they were right.
However, you will get undefined index notices in your form inputs, since those variables have not yet been set/defined.
View your HTML source (with error reporting enabled) and you will see something similar to the following:
<input type="text" name="main_email" required value="<br />
<b>Notice</b>: Undefined variable: email in <b>/path/to/your/file.php</b> on line <b>xxx</b><br />
" disabled></input>
and a similar notice for the other input.
The notices will appear in the inputs themselves.
Even though the input fields are disabled, PHP will still throw undefined notices for the variables.
Additional edit(s)
You stand at also getting Notice: Undefined variable: email in... and for the username after submission. Least, that's what my test revealed. Again; use a ternary operator for your inputs and it will solve the problem completely.
It is presently unknown as to why you're echoing the values for the inputs. If those are populated from elsewhere (a database, a file, other), either remove the variables, use a ternary operator as I already said, or use "Email" and "Username" as the default values.
Use a ternary operator as shown above.
http://php.net/manual/en/language.operators.comparison.php
Add error reporting to the top of your file(s) which will indicate errors, if any.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
if(isset($main_email))
{
echo $main_email;
}
you can use isset() function...
http://php.net/manual/en/function.isset.php
Hi guys i have code like this
include('db.php');
if (isset($_POST['save']) && $_POST['save'] == '') {
if(isset($_POST['misamarti'])){ $misamarti = $_POST['misamarti']; }
if(isset($_POST['teleponi'])) { $teleponi = $_POST['teleponi']; }
if(isset($_POST['posta'])) { $posta = $_POST['posta']; }
if(isset($_POST['paqsi'])) { $paqsi = $_POST['paqsi'];}
$query = "UPDATE contact SET `misamarti` = ".$misamarti.", `teleponi` = ".$teleponi.", `posta` = ".$posta.", `paqsi` = ".$paqsi." WHERE `contact`.`id` =1;";
$result = mysql_query($query);
}
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="about" value="template1" />
<input type="text" value="" name"misamarti" />
<input type="text" value="" name"teleponi" />
<input type="text" value="" name"posta" />
<input type="text" value="" name"paqsi" />
<input type="submit" value="" name="save" />
and result is
Notice: Undefined variable: misamarti in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
Notice: Undefined variable: teleponi in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
Notice: Undefined variable: posta in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
Notice: Undefined variable: paqsi in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
if anyone know why is this error pls comment.
You're using the variables even if they weren't submitted with the form, and update the fields in the db with those undefined varaibles, so you'll be destroying any data that was in the DB previously.
If nothing else, you need to at least set a default value for the fields, e.g:
$misamarti = isset($_POST['misamarti']) ? $_POST['misamarti'] : '';
^^--default empty string
And then realize that you are WIDE OPEN to an sql injection attack. You are BEGGING to get your server pwn3d.
As you can see in your HTML code, you have the names bad written, they have to be written this way: name="misamarti". You forgot the equal (=) between name and the name. If you see the rest of attributes, like text and value, you see that theres an equal, that's the correct way of setting attributes.
I am struggling with generating an output if none of the check boxes are selected from the code below.
HTML - Form
<label>Item 1:</label><input type="checkbox" name="selected[]" value="Item 1"/>
<label>Item 2:</label><input type="checkbox" name="selected[]" value="Item 2"/>
<label>Item 3:</label><input type="checkbox" name="selected[]" value="Item 3"/>
PHP Code
<?php
foreach ($_REQUEST['selected'] as $key => $selected) {
echo "$selected";
}
?>
The code outputs the correct value when selected, but generates the "Undefined index: selected in..." & "Invalid argument supplied for foreach():"
Can anyone point me in the right direction?
Thank you
You need to check the value is set before using it:
<?php
//$_REQUEST['selected'] is set and is array
if(isset($_REQUEST['selected']) && is_array($_REQUEST['selected'])){
//Loop it
foreach ($_REQUEST['selected'] as $key=>$selected) {
echo htmlspecialchars($selected);
}
}
?>
With Undefined index: extra that could be some other error, but you think it is this. any ways if you see an Undefined index warning that means your trying to access an array key that is undefined e.g not set.
FYI: Note that $_REQUEST will accept from $_GET and $_POST, if you are not expecting that value to be set from either, it would be better tobe more specific, so if you use POST in your form then use $_POST['selected']
I have a class member in PHP that looks like this:
function hidden($defaultSort=""){
$defaultSort = 'a';?>
<input type="hidden" name="sort" id="<?php print $this->id;?>sort" value='<?php print $defaultSort; ?>' />
<?php print $defaultSort;
}
When I call this function I get the following source in firefox
<input type="hidden" value="<br /><b>Notice</b>: Undefined variable: defaultSort in <b>/www/sptdev/htdocs/includes/v7/sptSearchBox.php</b> on line <b>24</b><br />" id="searchFormSearchsort" name="sort"/>a
Any ideas why I am getting the Undefined variable error?
More details:
The line 24 that the error is coming from is the line with the hidden input.
I have discovered that this works as expected in IE8.
<input type="hidden" name="sort" id="searchFormSearchsort" value='a' />a
This doesn't make much sense to me since PHP is server side and this should have nothing to do with the browser.
UPDATE -
The problem seems to be that I was highlighting and selecting "View Selection Source" in Firefox. When I just use "View Page Source" it looks fine.
Try:
function hidden($defaultSort=""){
$defaultSort = 'a';
echo '<input type="hidden" name="sort" id="' , $this->id , 'sort" value="' , $defaultSort ,'"/>';
print $defaultSort;
}
Edit:
I just tried this code and it works fine. Try to copy-paste it as it is and see if it works for you.
your code is correct. Probabily you're watching in wrong place (maybe you're modifying a parent class and an overrided method)