How to pass a value with the same PHP file? - php

I want to pass the same boolean value "isProvena" several times in two PHP files. The first time I pass the value from utilitizer.php to hraprint.php by using the codes as follows:
if ($_POST['type'] == 'printphys' || $_POST['type'] == 'printprovenahpa')
{
echo "<form action='/content/822' method=post>";
echo "<input type=hidden name=filename value='$filename'>";
if($_POST['type'] == 'printprovenahpa') {echo "<input type=hidden name=isProvena value='1'>";}
echo "<input type=hidden name=content value='";
if ($_POST['type'] == 'printphys') echo 751;
else if ($_POST['type'] == 'printprovenahpa') echo 520;
echo "'>";
echo "<input type=submit value='Start Job'></form>";
}
And then I get the value "isProvena" from hraprint.php and post(get) again:
$isProvena = false;
extract($_REQUEST, EXTR_IF_EXISTS);
$isProvena = (boolean)$isProvena;
<form action="/content/822" method="GET">
<input type="hidden" name="isProvena" value="<?php echo ($isProvena) ? '1' : '0' ?>" />
<tr>
<td><label for="showOnlyScreening">Print Only Screenings:</label></td>
<td><input id="showOnlyScreening" type="checkbox" name="showOnlyScreening" value="1" <?php echo ($isProvena) ? 'checked="checked"' : ''?>/></td>
</tr>
<tr>
</table>
</form>
And post again:
<form action="/content/822" method="POST">
<input type="hidden" name="isProvena" value="<?php echo ($isProvena) ? '1' : '0' ?>" />
</table>
And I do the judgement here:
if($isProvena){
.........
}
The reason I need to post(get) several times is that there are several page redirect action happens in the same PHP file(hraprint.php). When I was trying to get the value which is supposed to be 'true' from if($isProvena){} and execute the function, I failed.
Anyone can help me to have a look and tell me what is wrong?

It would be easier if you simply use sessions for that. Sessions are made specifically for that purpose - passing variables easily from one page to another.
And it is not yet established in the last code block of your answer that $isProvena already exists because I do not see any extract() there.
P.S. Use the $_POST and $_GET variables instead of extracting the $_REQUEST. The code is vulnerable to the problems caused by register_globals

Related

How to make a cookie save and get system?

I am making a system that saves someone's UDID (iOS) and outputs it back in the original input field.
It is for an iOS web-app I am working on. I have tried this:
<?php
$udid = $_POST['udid'];
if(empty($udid)){
$udid = $_COOKIE['udid'];
} else {
$udid = $_POST['udid'];
}
?>
<form class='form-horizontal well' action='#' method='post'>
<input type='text' name='udid' class='input-large' placeholder="udid" value='<?php $udid ?>'>
<button type='submit' id='submit' class='badge-primary'>Save</button>
</form>
<?php
setcookie("udid", $udid, time()+31536000000, "/");
?>
I need the user's udid to be saved and then outputted in the input field. When I ran this, the UDID didn't display in the input field. What have I done wrong?
I think you error is on the line below:
<input type='text' name='udid' class='input-large' placeholder="udid" value='<?php $udid ?>'>
Change it to:
<input type='text' name='udid' class='input-large' placeholder="udid" value='<?= $udid ?>'>
Notice I changed: <?php $udid ?> to <?= $udid ?>
This way your PHP script will echo the variable.
In fact, <?= is just a shorthand for <?php echo since php5.4
You have to echo the variable $udid
value='<?php echo $udid ?>'
Make sure the variable $udid not empty else the cookie never assigned.

Send form when with mysql_fetch_array while loop CLOSED

I have got a while loop that runs through all the records of the database printing them on a table. Now i also have some checkboxes within that very loop that I want to use to submit a form when clicked. Now when I click the checkbox it will indeed submit the form thanks to a Jquery script I found, BUT whenever i submit it it submits with the ID of the first record of the table.This Image
shows the table, as you see the first record has ID 34. Now every checkbox I click will send the $id 34.
This does not happen with normal submit buttons.
Is there a way I can submit with the individual userID's
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
I'm sorry if i'm not very clear with the explanation it is quite hard to explain this situation. Thank you guys!
Probably your problem is that you are submiting the same form always and its because you create a form for each row but it has the same id
For you the easy way is to put each form with the id cointaining the unique value of the row and doing submit with that.
Something like this
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete_<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete_<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
It looks like the <form> your creating has a static id, so ALL forms will have id='resComplete'. The jQuery submit function will grab the first element with id='resComplete' and submit it. You need to make it unique for every form and make the onchange='$("#resComplete").submit();' code match it.
Eg.
<?php
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete-<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete-<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
Better yet, use jQuery to find out what form it's in by chaning the onchange to something like:
<input id='complete' type='checkbox' name='complete' value='1' onchange='$(this).closest('form').submit();' <?php if($complete == 1){echo "checked";}?>>

php button call a php function

I have that code to create a button
$button = "<input type='submit' id='liga' value='liga'>";
echo $button;
I have the php function
function liga(){
....}
as I do so by clicking the button it calls the function?
Using html this code works, but I really need use the php button, how can I make it?
<input type="submit" name="liga" value="liga" />
if (isset($_REQUEST['liga'])) {
liga();
} elseif (isset($_REQUEST['desliga'])) {
desliga();
}
This little snippet will take an array with the index being the name of the button and the value as the label the button should have. It will then make a button for each element of the array.
<?php
$foo = array('name'=>'label', 'name2'=>'label2');
foreach ($foo as $k=>$v)
echo "<input type=\"submit\" name=\"$k\" value=\"$v\" />\n";
?>
You can also put the PHP variable right inside the HTML code using <?=...?>:
<input type="submit" name="<?= $myPhpVar ?>" value="<?= $myOtherPhpVar ?>" />
Or you can put complicated expressions (or whole programs) using the typical <?php...?> tags inside HTML brackets - whatever it echod becomes the content of that html tag:
<input type="submit" name="<?php echo $myPhpVar; ?>" value="<?php echo "LABEL: ".$myOtherPhpVar; ?>" />
I found a solution
echo "<input type='submit' name='liga' value='Liga'>";
echo "<input type='submit' name='desliga' value='Desliga'>";

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

A Simple HTML Checkbox with PHP Problem

I have a field in my update form called approve which is using the html checkbox element. now i am querying the approve value from the database which will hold the binary value (0 or 1), i want the checkbox to perofrm the following actions in condition.
a) While Querying from database.
1)if the value of active is 1 then it should be checked by default and also it should hold the value 1 to process it to update
2)the same applies for 0, if the value is zero then it is unchecked and will hold the value 0 to process
P.S: I want to use this for updating the form not inserting.
Do it like this:
PHP embedded in HTML way:
<input name="chk" type="checkbox" value="<?=$value?>" <?php if($value==1) echo 'checked="checked"';?> />
Pure PHP way:
<?php
echo '<input name="chk" type="checkbox" value="'.$value.'"';
if($value == 1)
echo ' checked="checked" ';
echo '/>';
?>
Just a 1-line shorter version ;)
<?php echo "<input name=\"chk\" type=\"checkbox\" value=\"$value\"".( ($value == 1) ? " checked=\"checked\"" : "" )." />"; ?>
Like this:
<input type="checkbox" value="<?php echo $row['approved']; ?>" <?php if($row['approved'] == 1): echo 'checked="checked"'; endif; />

Categories