Problem in sending values between pages in PHP - 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).

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

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

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

Passing data between PHP webpages from a dynamically generated list

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

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