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)
Related
I am working on a PHP project - I had one form post a date to another form
I made some changes (although none to the input in question)
Now all other inputs are updated with their Posted values, except the date
If I manually set the date in HTML it works:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="2009-01-01"></div>
If I set it to the following, it doesn't:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo (isset($DateCourse))?$DateCourse:'';?>"></div>
The below:
$DateCourse = ($_POST["DateCourse"]);
var_dump($_POST["DateCourse"]);
var_dump($DateCourse);
Returns:
string(10) "2019-01-05" - means the post value is set
Notice: Undefined variable: DateCourse in /home/bitecons/bts.biteconsulting.co.za/v2/editccr.php on line 119 - how can it be undefined, I just defined it
NULL
What on earth am I doing wrong? Apart from using PHP :P
Flow as requested:
Records.php:
This is the function to prepopulate my posted fields:
function Prefill(x) {
TabletoEdit = x.closest('table').id;
SelectedRow = x.rowIndex;
document.getElementById("EntryEditing").value = x.cells[19].innerHTML;
document.getElementById("DateCourse").value = x.cells[0].innerHTML;
document.forms["records"].submit();
}
Then I also have:
<form action="editrec" method="post" id="records">
<input type='hidden' name='Period' id='Period' />
<input type='hidden' name='Month' id='Month' />
<input type='hidden' name='res' id='res' />
<input type='hidden' name='CustName' id='CustName' />
<input type='hidden' name='DateCourse' id='DateCourse' />
</form>
The Prefill gets called, then submits the form
I have tracked and DateCourse has data, but when getting to the other form, it "disappears":
if(!empty($_POST)) {
$DateCourse = ($_POST["DateCourse"]);
$CustName = ($_POST["CustName"]);
}
For example, CustName is filled in, but not DateCourse?
Side question:
Would this return true if another post var is not set (unrelated to this one):
if(!empty($_POST))
I think You use wrong Code
you Must Submit First Form And Then Use $DateCourse this In another Form in POSTBACK
One of the best way is to define $DateCourse too like
<?php
$DateCourse = "";
if(!empty($_POST["DateCourse"])) {
$DateCourse = ($_POST["DateCourse"]);
}
?>
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo $DateCourse;?>"></div>
Okay, apologies folks, but it may help others in the future.
I had a function call to an old function - this failed, causing my variable to never get defined... I knew it was something stupid, but sometimes one needs a sound board...
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
im having this variable:
var setValuey = function(x) {
document.getElementById('atype').value= x;
}
this refers to a clicked element:
<input type="hidden" name="type" value="" id="atype"><div class="person"><img src="images/voksenknap.png" onClick="setValuey('voksen')">
the problem is now that the set value (which happens when you click the element needs to be send to a PHP form on another site called resu.php which has this form:
form method="get" action="koeb.php">
<fieldset><legend>Billet</legend> <table border="0"><tr> <td>Type</td><td> <input type="text" name="zone" value="<?php echo (?????);?>" readonly id="atype" style="margin-left: 20px"> </td> </tr>
how do i get the set value into the value field ? whenever i am writing something in the value="<?php echo (?????);?>" all i get is a text of an undefined variable or the text setValuey:
<br /><b>Notice</b>: Undefined variable: setValuey in <b>C:\xampp\htdocs\interaktion\01-company\resu.php</b> on line <b>18</b><br />
that is what is being written in the value field for example.
hope my description is understandable. i really need help with this.
The value is put into a form input. On the server, you read that with $_POST['type'], so it should be:
value="<?php echo $_POST['type']; ?>"
EDIT: Here is the code that sets the PHP variable "photofilename":
$photofilename = "C:\XAMPP\htdocs\ourWEbSite\images\coolPhoto.jpg"
I also tried this to make sure this wasn't a 'displaying a server file path in an input in the browser" security issue, and I get the same error message:
$photofilename = "HELLO"; // this gives the same error message!
ORIGINAL POST:
I'm trying to set the "value" on an input tag with a PHP variable and it is generating this error:
<br /><b>Notice</b>: Undefined variable: photofilename in <b>C:\XAMPP\htdocs\ourWEbSite\pageFour.php</b> on line <b>372</b><br />
Despite this error -- just one line above the html input tag that generates this error, I successfully use the exact same PHP variable in an img tag and the image appears on the page.
Here's the code:
<img id='theSelectedImage' src='<?php echo $photofilename ?>' />
<input type="text" id="theSubjectOne" style="width: 350px" value='Is-this-text-visible'/></br>
<input type="text" id="theSubjectTwo" style="width: 350px" value='<?php echo $photofilename ?>'/>
Here's what I see on the page:
(1) I see the photograph photofilename rendered successfully in the img tag;
(2) I see an input tag filled with 'Is-this-text-visible';
(3) I see an input tag filled with: Notice: Undefined variable: photofilename in C:\XAMPP\htdocs\ourWEbSite\pageFour.php on line 372
Is there something about not being able to set the 'value' of an input tag using a PHP variable? I know, with certainty, that the 'photofilename' PHP variable IS, in fact, defined, because I use it just above the input tag in an img tag and it renders correctly.
check your code if you don't get your variable repopulated somewhere between 1st and 2nd line of
<?php echo $photofilename ?>
the code you posted definitely works: check it on http://phpfiddle.org/
<?php $photofilename='lalala';?>
<img id='theSelectedImage' src='<?php echo $photofilename ?>' />
<input type="text" id="theSubjectOne" style="width: 350px" value='Is-this-text-visible'/></br>
<input type="text" id="theSubjectTwo" style="width: 350px" value='<?php echo $photofilename ?>'/>
Does it works like this ?
<?php $photofilename = "..."; echo $photofilename; ?>
If that all doesen't work just set it again before your variable it might seem retarded but if you need a quik fix oh well.
The second line ends with an incorrect tag: </br>. It should be <br>. Can't see why that would produce a PHP error, but it's a start.
I can't set a variable from a post array.
I have a simple form with a hidden field in it:
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
This hidden field gets sent off to a second file (exec.php) where I have the following code:
$sid = $_POST['sid'];
For some reason, when trying to set $sid, it gets a NULL value. For haha's, I ran the following:
foreach($_POST as $var => $value)
{
echo $var . ' : ' . $value . "<br>";
}
This provided a correct value of 1938 for sid. I've looked at this for 3 hours and can't find what is happening. I expect something extremely stupid...any thoughts?
Here is the form on enter.php
<form name="form1" method="post" action="exec.php">
<input name="sid" type="hidden" id="sid" value="<? echo($sid); ?>">
<input name="ticket_totals" type="hidden" id="ticket_totals" value="<?=$ticket_totals?>">
<input name="emp" type="hidden" id="emp" value="<?=$emp?>">
<input name="submit" type="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Close">
</form>
Here is the POST output on exec.php:
type : Other
ticket_totals : 0
emp : 105
sid : 1939
submit : Submit
Okay - this was poor syntax on my part but now I'm curious as to why.
I left out quotation marks - the solution is as simple as this:
$sid = $_POST["sid"]
Now it works like a champ.
Any takers on why? I'd guess there is a setting in the php.ini that requires the quotes. Strangely enough, I have other variables called from the POST array that i'm not using quotes for and they're working fine...
Use Console in FireBug to inspect the POST request to see what is the sid value that is being sent.
If the sid value in request is ok, use var_dump($_POST["sid"]); to see the results on the server.
EDIT: it's considered good PHP style to use the quotes when accessing the associative array because quote-less keys are indistinguishable from constants:
define('myVar',3);
echo $array[myVar]; // retrieves $array[3], not $array['myVar'];
Try to echo the $sid instead of the <?=:
// Change that
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
// With that
<input name="sid" type="hidden" id="sid" value="<?php echo $sid; ?>">
also for the test time try to change the input type from hidden to text in order to be 100% sure the $sid contains a value.
Using quotes for associative array keys is mandatory, and while it may work without them, it's incorrect and erratic behavior is expected.
I had this same problem, trying to use $_POST[sid] as a variable. I'm am thinking that "sid" is a reserved or restricted variable name, because I changed my variable to $_POST[snid] and it worked just fine. This was my code
$sid = $_POST[sid];
$recipient = "($sid) ($_POST[sid])";
if ($_POST[sid] > 0)
{
$recipient = "It Worked";
}
print $recipient;
When I posted "&sid=15", the result was:
() (15)
Unbelievable. Impossible, right? All I did was change from using "sid" as the index to "snid", and it worked no problem.
So, don't ever use $_POST[sid].