Hidden value requires to be send twice - php

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

Related

How to make a checkbox checked and disabled?

I have tried:
<input disabled='disabled'
checked='checked'
type='checkbox'
name='checkTema[]'
value=".$cat->cat_ID."> ".$cat->name."<br>"
But that won't pick up the value when I send the form
UPDATE
Thanks to a comment I've got a helping answer from another SO question, yet I am not sure how I should be applying the trick, tried the following but not sending the value
echo "<input type='hidden' name='checkTema[]' value='1' >";
echo "<input type='checkbox' checked='checked' disabled='disabled' name='checkTema[]' value=".$cat->cat_ID."> ".$cat->name."<br>";
Try return false in onclick event
<input
onclick="return false"
checked='checked'
type='checkbox'
name='checkTema[]'
value='.$cat->cat_ID.'> ".$cat->name."<br>"
If you need to POST disabled checkboxes, I suggest using a hidden input before:
<input type="hidden" name="checkbox_disabled" value="<?php echo $checkbox_value; ?>" /> <== Checked and disabled
Then in your PHP code, you can simply check:
if ($_POST['checkbox']) {
//Checkbox wasn't disabled on submission
} else {
//Fallback to using the disabled value:
$disabledVal = $_POST['checkbox_disabled'];
}
Additionally, you can simply undisable all the elements of the form before submission:
$('form').submit(function() {
$(this).find('input').attr('disabled', false);
});
See this question/answer for more information on how you can alternatively do this.
Try this code :
<input disabled='disabled' checked='checked' type='checkbox' name='checkTema[]' value="<?php echo $cat->cat_ID;?>"><?php echo $cat->name;?>
Thanks to a comment, I've got the solution in another SO question
Then my code changed to:
echo "<input type='hidden' name='checkTema[]' value=".$cat->cat_ID." >";
echo "<input type='checkbox' checked='checked' disabled='disabled'> ".$cat->name."<br>";

php: print out array within form containing empty space

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

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

PHP SQL UPDATE works in FF and CHROME but not in IE?

the below code works perfectly in FF and CHROME but not in IE. Please help. I have commented out my santize functions as i thought they might be affecting it, but it still does the same.... nothing in IE.
Thank you in advance for any assistance.
<?php
//IF UPDATE BUCKET CHANGE STATUS...
if(isset($_POST['updatebucket'])){
$complete = $_POST["complete"];
$bucketid = $_POST["bucketid"];
//$complete = sanitizeone($_POST["complete"], "plain");
//$complete = strip_word_html($complete);
//$bucketid = sanitizeone($_POST["bucketid"], "plain");
//$bucketid = strip_word_html($bucketid);
if ($complete=="1")
$complete = "0";
else
$complete = "1";
$updatebucket = "UPDATE membersbuckets SET complete = '$complete' WHERE userid = '$userid' AND bucketid = '$bucketid'";
mysql_query($updatebucket);
}
?>
and the front end....
<? if ($complete=="1") {
echo "<form action='' method='post' name='updatebucket'><input name='complete' type='hidden' value=" .$complete. " /><input name='userid' type='hidden' value=" .$userid. " /><input name='bucketid' type='hidden' value=" .$bucketid. " /><input type='image' name='updatebucket' value='updatebucket' src='images/tick.png' /></form>";
}else{
echo "<form action='' method='post' name='updatebucket'><input name='complete' type='hidden' value=" .$complete. " /><input name='userid' type='hidden' value=" .$userid. " /><input name='bucketid' type='hidden' value=" .$bucketid. " /><input type='image' name='updatebucket' value='updatebucket' src='images/cross.png' /></form>";
}
?>
Dan
You should post your front-end, not back-end (since it's pretty much not browser-dependant).
Your HTML probably isn't valid.
Edit:
Yep, IE doesn't take value for image type of input. It only sends the x & y (field_name_x, field_name_y) and totally discards the original "value" attribute.
Try with a hidden input instead.
It seems that input type='image' doesn't send the value when used from IE. You'll need another hidden field:
<input type='hidden' name='updatebucket' value='updatebucket' />
<input type='image' src='images/tick.png' />
That way, the updatebucket parameter will be posted to the server, regardless of the browser used.
The assumption here was that all browsers handle HTML forms the same way (and they don't); that's why I keep Eric Lawrence's excellent Fiddler around - it can diff two HTTP requests, so you'll see the difference between the browsers immediately.
An alternative would be to check for $_POST[{image-element-name}_x}] (in this case $_POST['updatebucket_x']. All browsers will send the x/y coordinates of the image element as updatebucket.x & updatebucket.y, and PHP silently (and frustratingly) alters the updatebucket.x to updatebucket_x. Then again, you only need this is clicking different input type=submit / type=image elements would alter the action taken, otherwise the previous solution of a hidden element as previously suggested would do.

Categories