How to make a cookie save and get system? - php

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.

Related

Passing php variable as hidden input where html is contained in one echo

<?php
if(isset($_POST['btnLogin'])){
$myVariable = $_POST['fieldParameter'];
if(condition){
//do something
}else{
echo "
<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>
";
}
}
?>
Notice that the variable $myVariable is contained in the main IF block. I'm trying to send the value of $myVariable to submit.php as hidden field.
Also, i enclosed all the html tags using one echo statement with double quotes.
I found related questions here in SO but can't find similar to embedding php within a long echo of html tags
I tried to put value='<?php echo $studentNo; ?>' with no success.
I want to access it in a submit.php file like this,
submit.php
<?php
$aVariable = $_POST['myVariable'];
echo $aVariable;
?>
How can I pass the value contained in $myVariable as hidden field? Is there something wrong with the way I use double and single quotes?
If you are already echoing a string you shouldn't put <?php echo "" ?> inside it again. You should concatenate your string instead. But in your case you don't even need to do that, because you're using double quotes for echoing which means you can simply just write your variable in it.
echo "<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='$myVariable;'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>";
If you were using single quotes for your echo, it would look like this:
echo '<form method="POST" action="submit.php"><br/>
<input type="hidden" name="myVariable" value="' . $myVariable . '"/><br/>
<input type="submit" name="btnSubmit" id="submit" value="Submit">
</form>';
You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the
You can try these variants (simplified):
// code before
echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
// code after
// OR //
// code before
?>
<input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
<?php
// code after
Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).

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

POST throwing undefined index on every var

I'm defining POST here:
else if(isset($_POST["Edit"])){
$result_set = $sqlConn->query("SELECT Art, Ime, Cijena, Kol FROM permatable WHERE Art='{$_POST["Art"]}' LIMIT 1");
$Item = $result_set->fetch_array();
$_POST["Art"] = $Item["Art"];
$_POST["Ime"] = $Item["Ime"];
$_POST["Cijena"] = $Item["Cijena"];
$_POST["Kol"] = $Item["Kol"];
header("Location: EditP.php");
}
Now the Post "Edit" and the if statment is being called just fine. I've also checked the query and it returns everything it needs to.
Now on the header location I get undefined index on all of these vars and thats coming from the following code:
echo "<input type='text' placeholder=".$_POST["Art"]." name='Art'>
<input type='text' placeholder=".$_POST["Ime"]. "name='Ime'>
<input type='text' placeholder=".$_POST["Cijena"]." name='Cijena'>
<input type='text' placeholder=".$_POST["Kol"]." name='Kol'>";
Am I doing something wrong? I can't see any mistakes myself and this has been bothering me for a while now.
That won't work.
you can't send POST with header location
you can try to add them to your url
foreach($item as $key=>$value)
{
$items .= $key."&".$value ;
}
header("Location: EditP.php?".$items);
and in the edit.php page
echo "<input type='text' placeholder=".$_GET["Art"]." name='Art'>
<input type='text' placeholder=".$_GET["Ime"]. "name='Ime'>
<input type='text' placeholder=".$_GET["Cijena"]." name='Cijena'>
<input type='text' placeholder=".$_GET["Kol"]." name='Kol'>";
You cannot redirect the user w/ POST variables. It will not work. Read here. The best way to do it is to create a form and then automatically submit it via JavaScript as soon as the page loads. Example:
<form id="my_form" action="http://example.com" method="post">
<input type="hidden" name="name" value="<?php echo htmlspecialchars($name); ?>">
<input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
<input type="hidden" name="etc" value="<?php echo htmlspecialchars($etc); ?>">
</form>
<script type="text/javascript">
//Our form submission function.
function submitForm() {
document.getElementById('my_form').submit();
}
//Call the function submitForm() as soon as the page has loaded.
window.onload = submitForm;
</script>
$result_set = $sqlConn->query("SELECT Art, Ime, Cijena, Kol FROM permatable WHERE Art='($_POST["Art"])' LIMIT 1");
Try changing curly braces to small braces.
Well, I used SESSION at the end so I just changed the code to following:
$result_set = $sqlConn->query("SELECT Art, Ime, Cijena, Kol FROM permatable WHERE Art='{$_POST["Art"]}' LIMIT 1");
$Item = $result_set->fetch_array();
$_SESSION["Art"] = $Item["Art"];
$_SESSION["Ime"] = $Item["Ime"];
$_SESSION["Cijena"] = $Item["Cijena"];
$_SESSION["Kol"] = $Item["Kol"];
header("Location: EditP.php");
And for the EditP:
echo "<input type='text' placeholder=".$_SESSION["Art"]." name='Art'>
<input type='text' placeholder=".$_SESSION["Ime"]. "name='Ime'>
<input type='text' placeholder=".$_SESSION["Cijena"]." name='Cijena'>
<input type='text' placeholder=".$_SESSION["Kol"]." name='Kol'>";

How to pass a value with the same PHP file?

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

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