how to change input value via url dynamically - php

I am using this url to change value of Price in my code.
http://yoursite.com/mycart.php?am=20&product=Digitizing
I want that whenever I change the value of am= in the url , the value='0' must be changed in the code dynamically according to that.
<input type='hidden' name='li_1_price' value='0' />

You just need to intercept the value with PHP and then write the variable to the code.
<?php $amValueIs = $_GET["am"]; ?>
Then in your HTML
<input type='hidden' name='li_1_price' value='<?php echo $amValueIs; ?>' />
That's all there is to it.
EDIT as per comments:
If you want to be sure that "am" has a value so you don't kick up a warning:
$amValueIs = isset($_GET["am"]) ? $_GET["am"] : "0";
Which is a shorthand for "if it's set then use it, if not give it a 0".
I also added that "php" to the other open tag, just in case.

Related

Input type image is not working in firefox

I have some problem.
Firefox can't read the value and name of input type image.
Here is my piece of code:
*blabla.php
<?php
echo"
<form action='cek.php' method='POST'>
<input type='image' src='something.png' alt='Submit button' value='Continue' name='noi'>
</form>
";
?>
*cek.php
<?php
echo"
$a = $_POST['noi'];
<input type='text' value='$a'>
";
?>
but Firefox says there is no input named 'noi'.
Please help me to solve this problem.
Thanks in advance.
What you need to do and am under the impression you want the word "Continue" to appear in the text box.
At least that's the impression I am getting by the use of value='$a' which tells me that you wish to echo that variable from your form.
If so, then you need to modify your PHP handler to:
<?php
$a = $_POST['noi'];
?>
<input type='text' value='<?php echo $a ?>'>
and adding PHP tags while echoing the value for it, using:
value='<?php echo $a ?>'
as opposed to: (which will cause a parse error)
echo"
$a = $_POST['noi'];
<input type='text' value='$a'>
";
since there is a semi-colon after $_POST['noi'] then trying to inject HTML into without going out of and back into PHP. A semi-colon tells PHP to stop. Yet since there was no closing PHP tag following the semi-colon and no re-opening tag, PHP throws that (parse) error.
That is the reason you are getting the undefined error message.
Having error reporting "ON", would have signaled this "parse" error.
Place the following at the top of your file(s) during development.
error_reporting(E_ALL);
ini_set('display_errors', 1);
which will signal errors, if found in your code.
Footnote:
Alternatively, you can use:
<?php
$a = $_POST['noi'];
echo"
<input type='text' value='$a'>
";
?>
simply by moving the $a = $_POST['noi']; outside the echo.
if you want to send "Continue" to the *cek.php script, you can use an html input element with type hidden and value "Continue" in form on *blabla.php
Submit button with type image only send x and y co-ordinates of the button (NOT its value).
So, in your *blabla.php:
<?php
echo"
<form action='cek.php' name='myform' method='POST'>
<input type='text' name='first-name' id='first-name' value='' />
<input type='hidden' name='my-input' value='Continue' />
<input type='image' src='submit.png' alt='Submit button' value='Continue' name='noi'>
</form>
";
?>
and in *cek.php
<?php
$a = $_POST['my-input'];
echo "<input type='text' value='$a'>";
?>
When using input type=image you are telling the browser to submit the x and y coordinates where the user clicked on the image. This allows you to add image map type functionality to your forms. Specification says:
“the element's value attribute must be omitted“
But browsers are inconsistent about that, some browsers like Chrome will send it, and some like Firefox follow the specifications and will send only the X and Y coordinates. That is the reason why there is no input named 'noi' in your example.
To learn how to inspect variables check the PHP documentation for functions like var_dump, var_export, print_r, isset etc. Put this code in cek.php, submit the form and you will see the $_POST array values.
<?php
var_dump( $_POST );
// Firefox:
// array(2) { ["noi_x"]=> string(3) "134" ["noi_y"]=> string(2) "91" }
// Chrome:
// array(3) { ["noi_x"]=> string(3) "121" ["noi_y"]=> string(2) "93" ["noi"]=> string(8) "Continue" }
?>

$_POST variables missing when adding HTML checkbox array

When a process changed required me to change a radio button field, to a checkbox to allow multiple selections, I made the following change to my html:
<input type='checkbox' name='ptype[]' value='1'> Jail/not sentenced</br>
<input type='checkbox' name='ptype[]' value='2'> Jail/Sentenced</br>
<input type='checkbox' name='ptype[]' value='3'> State/DOC</br>
<input type='checkbox' name='ptype[]' value='4'> ICE/US Marshall</br>
<input type='checkbox' name='ptype[]' value='5'> 7x/wardens Agree</br>
When making ptype an array to handle multiple selections, I am finding my $_POST variable missing a key. If I try to revert ptype to a radio button and handle only one value, I don't get any error/warning.
I've checked Firebug, and when ptype[] is set, one of my $_POST variables is not relayed. I know the max post variable is not an issue, as I only have 52 post variables on my form.
I don't know if it's relevant, but this field is never coming through:
<input type='radio' name='wc' value='1'> Yes
<input type='radio' name='wc' value='0'> No
Notice: Undefined index: wc in C:\inetpub\wwwroot\internal_tools\include\consults\consult_utilities.php on line 53
Any help would be greatly appreciated.
EDIT: As requested, my form.
EDIT 2: Firebug POST variables
EDIT 3: Added line 53:
$data['workers_comp'] = $_POST['wc'];
Probably you didn't select an option for the wc radiobutton and therefore the variable is not submitted.
You should change your PHP code to:
$wc = isset($_POST["wc"]) ? $_POST["ws"] : "0";
Or, as I already suggested in comment, you have a problem in the javascript method validateFrm which you call upon submitting the form.

UPDATE database and update where variable

I have a question in regards to the GET function. I currently have a form with a get action. I am accessing that data in another page with the get function. Unfortunately, I have been getting an "error: undefined index" on every one of my values. After reading up on similar problems, I tried using isset (as seen below) and it gets rid of the error, but I am not sure that my data is stored in the variables at all because if I echo the 4 variables, nothing shows up. Could someone give me a push in the right direction?
Data from form:
while($row = mysql_fetch_array($result)) {
echo "<div class=\"addform\"><form method='GET' action=\"update.php\">\n";
echo " <input type=\"text\" value=\"".$row['tfid']."\" name=\"tfid\">\n";
echo " <input type=\"text\" name=\"fname\" value=\"".$row['fname']."\"/>\n";
echo " <input type=\"text\" name=\"lname\" value=\"".$row['lname']."\"/>\n";
echo " <input type=\"text\" name=\"hca\" value=\"".$row['hca']."\"/>\n";
echo " <input type=\"text\" name=\"file\" value=\"".$row['file']."\"/>\n";
echo " <input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n";
echo "<img title='Delete Row' alt=\"Delete\" class='del' src='images/delete.png'/></form></div>\n";
}
echo "</table><br />\n";
and the code for retrieval:
$tfid= isset($_GET["tfid"]) ? $_GET["tfid"] : NULL;
$fname = isset($_GET["fname"]) ? $_GET["fname"] : NULL;
$lname = isset($_GET["lname"]) ? $_GET["lname"] : NULL;
$hca = isset($_GET["hca"]) ? $_GET["hca"] : NULL;
echo $tfid;
echo $fname;
echo $lname;
echo $hca;
After your edit:
After you posted your form code, I noticed the problem may be that the values of your inputs may be empty, as the syntax looks more or less correct. Double check the values via view-source to ensure their validity.
Before your edit:
The errors are ocurring because the variable in fact is not set. Make sure your form looks like this:
<form action="yourpage.php">
<input name="fname" type="text" />
<input name="lname" type="text" />
<input name="hca" type="text" />
<input name="tfid" type="text" />
</form>
As shown above, the _GET variables are looking for the name attribute. A common mistake among beginners (everyone has been guilty of this at one time or another) is the use of id in place of name. id is used for selector purposes whereas name is often used to retrieve values on the server side.
Something to keep in mind....
As #George Cummins mentioned, you should also be aware that $_GET parameters are passed through the URL like so:
http://yoururl.com?var1=data1&var2=data2
In some cases, you may not want the user to see all of the data being sent through your form, so in this case you would want to use $_POST which is essentially a hidden passing of the form's data. In reality, this data is not truly hidden but it certainly is more hidden than the use of $_GET.

Activate action onClick in PHP

I have the following chunk of code:
while ($row = mysql_fetch_array($result)) {
echo "<li/>".$row['trendName']."<input type='submit' name='Dispay' value='Display All Items' onclick='action='http://blah.co.uk/DisplayData.php';'/>";
echo"<br />";
}
So, this I want to do is for each element on the list to create a button. When I will click at this button I want to hit a URL.
Let's say that we have only one element on the list (Movies). By clicking on the button next to the movies, I want to move to another URL: http://blah.co.uk/DisplayData.php
and furthermore to pass to the other URL the name of the element (i.e. Movies)
Is this possible, and if so, how?
I would also recommend to use <button></button> in html 5 to create a button.
echo "<button type='submit' name='Display' onclick='javascript:location=\"http://blah.co.uk/DisplayData.php?value=$row['trendName']\"'>Display</button>";
As suggested above, a link is the proper way to do so.
<?php echo "<a href='http://blah.co.uk/DisplayData.php?value=".$row['trendName']."'>Display</a>"; ?>
You can use $_GET['value']; to retrieve your data "trendName"
However "action" is used in the <form></form> tag
<form action='http://blah.co.uk/DisplayData.php'>
<input type='hidden' name='value' value='<?php echo $row['trendName']; ?>' />
<button type='submit'>Display</button>
</form>
here you would use $_POST['value']; to retrieve your data "trendName"
onclick="this.location.href='http://blah.co.uk/DisplayData.php?value=' + $row['trendName']"
If you tell, where "someValue" should come from, I'll edit this code later.
However, I would recommend using a link instead.

Inserting a id value into a <a> tag?

i wanted to input a id value into a tag, where the value is hidden but i want to pass that value to another page . I have the code for input type however i have no idea how to do it on a tag.
codes for input type:
<input type='hidden' name='id' value='$id'/>
<input type='submit' value='More details' />
how to archieve the same effect on tag?
There's no way to pass information in a link (an <a> tag) besides putting it in the URL.
You can put an onclick event on the specific tag that changes the hidden input fields value. e.g.
Test
function setHidVal(idVal)
{
$(function(){ $('#myHiddenField').val(idVal); }); //You don't have to use jquery
}
So once posted, you can request the hidden field name to get value.
what is the problem? any value of a form`s input, even in a hidden input you find in
$_POST[id]
You can append in a tag like this.
Method 1:
<a href="your-page.ext?id=<? echo $id; ?>">
Method 2:
<form name="hidden_form">
<input type='hidden' name='id' value='$id'/>
</form>
<a href="your-page.ext" onclick="javascript:return submit_frm(this)" > some thing goes here</a>
<script>
function submit_frm(aelem) {
document.hidden_form.action = aelem.href;
document.hidden_form.submit();
return false;
}
</script>

Categories