PHP MySql $_GET problems - php

I have tried to search through the forums but I am a novice and am getting more confused.
I am trying to bring an input from a form and use it as a variable in a MySql query. A shortened version of the form is -
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input value=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
I am then trying to store the input into a variable using code -
$newVar = $_GET['leave'];
However I am getting an undefined index error.
Can anyone help with this? Sorry if its a very basic problem :)

The problem is with your HTML. You need to name the input.
echo '<input name="leave" class="text" autocomplete="off" type="text" value="' . $_SESSION['leave'] . '" />';

You declaring the "value attribute twice, you need to declare name:
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input name=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";

echo '<form method="get" action="">';
echo "<tr><td>Leave:</td><td><input value='{$_SESSION['leave']}' class='text' autocomplete='off' type='text' name='leave'/></td></tr>";
echo "</form>";
If you use single quotes and doubles quotes alternating, you can make your code look nicer.
For your problem, you're missing your input name:
<input type=".." name="leave" ..>
Also, notice in your output of the field, you have the value set to the session value and an empty value near the end.
value=\"\"

Related

Hidden value requires to be send twice

Basically I make a calculation and use a hidden value to put it into an input field.
So I've tried to recreate my problem in the code below as to not give away the actual code I'm working with since it's a bit more sensitive.
The question is whether it's possible to get the hidden value inside the disabled box without having to click the send button twice.
If I'm asking the impossible just say so I'll figure something out.
<form action='test.php' method='post'>
<?php
#$result = #$_POST['number1'] * #$_POST['number2'];
echo "<input type='text' name='number1'>
<input type='text' name='number2'>
<input type='text' value='"; if(isset($_POST['value'])) echo $_POST['value']; echo"' disabled>
<input type=hidden name='value' value='" . $result . "'>"
?>
<br>
<input type=submit>
</form>
do you expect the $result in the disabled input if the $_POST["value"] is not set?
echo "<input type='text' value='" .(isset($_POST['value'])?$_POST['value']:$result)."' disabled>";

want to print $ in html

i want to print a input value using html.
input will be like below..
<input type='text' name='tr_id' class='form-control' id='tr_id' style='height:35px;width:200px;' value='<?php echo $row['$tr_id'];'>
i am using below code to get the required input
$field_name='tr_id';
$field_type='text';
$html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='<?php echo $row['".field_name."']'>";
echo $html;
but i think i am getting error on value. can you please help how can i print
Your intent is not so clear, however it might be logical to assume that you may really have a typo in your PHP Code where you were echoing out the field_name.
Here is a correction on that part.
<?php
$field_name ='tr_id';
$field_type ='text';
$html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='{$row[$field_name]}' />";
echo $html;
Notice that the line: $html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='<?php echo $row['".field_name."']'>";
has changed to reflect what is presumably what you intended to do: $html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='{$row[$field_name]}' />"
The reason for this is because you are already in PHP Mode so you don't need to do something like <?php echo $row...?> within a PHP block... (meaning: you don't open & close a PHP Block within an already existing PHP Block) and, in fact, since you are doing echo $html it would be pointless to echo anything again within the code that builds up your string. The Point here is that you should rather build-up your String without echoing anything and then finally echo $html once you are done building up your HTML String. This way you'd avoid the Errors you got.
try below code..
<input type='text' name='tr_id' class='form-control' id='tr_id' style='height:35px;width:200px;' value='<?php echo $row[$tr_id];'>
And
$field_name='tr_id';
$field_type='text';
$row = array('field_name','test');`enter code here`
$html .= '<input type ="'.$field_type.'" name="'.$field_name.'" class="form-control" id="'.$field_name.'" style="height:35px;width:200px;" value="'.$row["field_name"].'" >';
echo $html;

Using PHP $_POST to fetch input from another PHP script's echo form

I understand the title is pretty ambiguous so let me explain in more detail. I am using PHP to output a form which will display certain user information, the information is that which the user had previously stored in the database. This part of the code is working fine and the data is being displayed as I wish. Once the data is displayed in the input boxes, I want the user to be able to edit the information and then once the user clicks the submit button the updateuserinfo.php script will be called:
while($return = mysql_fetch_assoc($result)) {
echo "<form action=UpdateUserInfo.php method=post>";
echo "<p> First Name: <input type=text name=firstname value= ".$return['FirstName']."/></p>";
echo "<p> Surname: <input type=text value= ".$return['Surname']." /></p>";
echo "<p> House Number: <input type=number value= ".$return['HouseNumber']." /></p>";
echo "<p> Address Line One: <input type=text value= ".$return['AddressLineOne']." /></p>";
echo "<p> Address Line Two: <input type=text value= ".$return['AddressLineTwo']." /></p>";
echo "<p> County: <input type=text value= ".$return['County']." /></p>";
echo "<p> Phone Number: <input type=number value= " .$return['PhoneNumber']." /></p>";
echo "<p> Email: <input type=email value= ".$return['Email']." /></p>";
echo "<p> Username: <input type=text value= ".$return['Username']." /></p>";
echo "<p> Password: <input type=password value= ".$return['Password']." /></p>";
echo "<p> User Type: <input type=text value= ".$return['UserType']." /></p>";
echo "<input id=submit type=submit value='Update Info' Info />";
echo "</form>";
In the script found in updateuserinfo.php I was hoping I could firstly display the users info i.e what the user has changed the input box values to and then update the database if the user confirms the changes. I am confident I will be able to update the information stored on the database as this is a simple SQL query but the problem I am having is that I can not display the information found in the input boxes when the user has made changes. I thought I could apply a name attribute to each input tag, like I have done with 'firstname' and then use:
echo "<p>First Name: " $_POST[firstname] "</p>";
in the updateuserinfo.php in order to display the information the user has entered in the input box. However this is not working. Any suggestions would be greatly appreciated as I'm extremely new to programming in PHP.
Thanks in advance.
Try to show the complete $_POST array by using this code:
var_export($_POST);
Also you are missing some quotes in your Form. For example the line
echo "<p> First Name: <input type=text name=firstname value= ".$return['FirstName']."/></p>";
should be
echo "<p> First Name: <input type=\"text\" name=\"firstname\" value=\" ".$return['FirstName']."\"/></p>";
or easier within single quotes (so you don't have to mask each double quote):
echo '<p> First Name: <input type="text" name="firstname" value= "'.$return['FirstName'].'"/></p>';
Read What is the difference between single-quoted and double-quoted strings in PHP? if you want to know what's the difference between single and double quotes in PHP
And in your update Script you are missing the dot operator and also the quotes:
echo "<p>First Name: " $_POST[firstname] "</p>";
should be
echo "<p>First Name: " . $_POST["firstname"] . "</p>";
You should read $_REQUEST array, not $_POST...
UPDATE
Sorry, I did never use $_POST... It has been introduced in 4.1.0.
So, only check your PHP version is >= 4.1.0...
Otherwise please post
print_r($REQUEST);
results...

Input value invisible when set with php

PHP
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<tr>";
for($i=0;$i<7;$i++){
if($i==0){
echo "<td><input type='text' val='$row[$i]' /></td>";
}
if($i>1){
echo "<td>$row[$i]</td>";
}}echo "</tr>";
}
Except for the first column, which contains the input tag, everything comes out fine. The input tag shows up, but it appears empty--nothing to see, nothing to highlight. If I do 'inspect element', however, I see it has the correct value according to the output from the queried table.
Any thoughts as to what causes this strange behavior and how to fix it?
You have to use basic HTML Tag
You have to use "value" instead of "val"
See the Basic HTML Tag
<input type="text" value="Nikunj"/>
You should use value attribute instead of val:
echo "<td><input type='text' val='$row[$i]' /></td>";
-----------------------------^
should be:
echo "<td><input type='text' value='$row[$i]' /></td>";
-----------------------------^
Change val to value. val is not a valid attribute.
<input type='text' val='$row[$i]' />
--------------------^

Problem in sending values between pages in PHP

I want to send data from one page to the other via a form in PHP. In the initial page I have included the following PHP script that generates an input form with hidden fields. The names of these hidden fields are generated by the PHP:
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='$software'></input>";
echo "<input type='hidden' name='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
In the second page, named process-results.php, I would like to get the names of these hidden fields via the $_GET method but of course using $_GET[$software] and $_GET[$version] wouldn't work...Can someone tell me if there is a solution to this issue or if there is a better alternative? Thanks in advance
Instead of
"<input type='hidden' name='$software'></input>";
you should use
"<input type='hidden' name='software' value='".$software."'></input>";
for each. This way, you can use $_GET['software'] to retrieve the value. Do this for each of your hidden inputs.
I think you may want something like:
<form ... >
<input type="hidden" name="software" value="<?php echo $software ?>" />
<input type="hidden" name="version" value="<?php echo $version ?>" />
</form>
and then
$_GET['software'];
$_GET['version'];
I'm not sure what you're trying to accomplish, but this looks odd to me. Isn't the below code more of what you're looking for?
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='software' value='$software'></input>";
echo "<input type='hidden' name='version' value='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
That way you will get a query string in form of ?software=yoursoftwarename&version=yourversion and it will be available via $_GET["software"] and $_GET["version"] on the next page.
You could iterate over each of the items in the $_GET array on process-results.php. The problem is that the keys for the value will be whatever $software and $version are set to on the first page. Try something like this:
foreach($_GET as $key=>$string) {
// Do stuff with them
}
Add
enctype="multipart/form-data"
To the tag... so it looks like
<form enctype="multipart/form-data" method......
If you really need to have the dollar-sign inside the name, escape it:
echo "<input type='hidden' name='\$software'>";
or put the string in single-quotes:
echo '<input type="hidden" name="$software">';
Otherwise PHP is looking for a variable named "$software", if you look inside the browser-source you will see that the name-attributes are empty(except you're having those variables defined somewhere).

Categories