php: print out array within form containing empty space - php

I try to display an array within a form, which works fine, except of the array value containing an empty space 'Street Number', then it only displays the Street.
If I echo it outside of the form it works, but not within the form and the loop:
//This is able to show Street and Number
echo $Kundendatenarry[4];
//within the loop and the form it is not working anymore it is only shwoing the street:
echo"<form name='form1' method='post' action='KundeundAutoBearbeiten_Update.php' accept-charset='UTF-8'>";
for ($i=1, $max=$Kundendaten->FieldCount(); $i < $max-3; $i++)
{
echo"<pre><input size='50' name='name' type='text' id='name' value=".$Kundendatenarry[$i]."></pre><br>";
}
echo"<input type='submit' name='senden' value='Daten Ändern'><br>";
echo "</form>";
I thought "< pre >" could help, but it didn't .
Can anybody tell me what I did wrong?
These are the db entries:
echo $Kundendaten
ID,Titel,Vorname,Nachname,Strasse_Hausnummer,Postleitzahl,Stadt,Telefon,EMail,Kommentar,Weihnachtskarte,Erzeugt,Geaendert 11111,,Kurt,Heiz,Rumpenheimerstraße 121, 15625,Offenbach,,,,0,,
--> It is printed perfektly except of Rumpenheimerstraße 121 --> here it prints out Rumpenheimerstraße and not the 121

There is no string tags around your input value:
echo"<pre><input size='50' name='name' type='text' id='name' value=".$Kundendatenarry[$i]."></pre><br>";
should be:
echo '<input size="50" name="name" type="text" id="name" value="'.htmlspecialchars($Kundendatenarry[$i]).'"/><br>';
I've also added htmlspecialchars() too as its common practice to prevent user data breaking the html

Try using this get result first then echo that result in your html script
<?php
foreach(condition){
?>
<form name='form1' method='post' action='KundeundAutoBearbeiten_Update.php' accept-charset='UTF-8'>
<pre><input name='name' type='text' id='name' value="<?php echo $Kundendatenarry[$i] ?>"></pre><br>
<input type='submit' name='senden' value='Daten Ändern'><br>
</form>
<?php
}
//foreach ends
?>
Strange error, but you can slove using it

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>";

Echoing Value to HTML Text Input

Okay, so typically I would write the following:
<input type='text' class='form-control' name='name' value='<?=$user['name'];?>'>
However, because I am using ' in my HTML, and if the name has a ' in it, (i.e. the last name is O'Brian for instance) It doesn't echo correctly, because the value is ending the input abruptly.
Of course a simple solution is to use " quotation marks with my html, but that doesn't help - because what about when I want to echo quotation marks as well? What can I do?
Use <input type='text' class='form-control' name='name' value='<?php echo htmlentities($user['name'], ENT_QUOTES); ?>'>

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...

PHP MySql $_GET problems

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=\"\"

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