I'm calling a function from an input radio button and have this:
onClick=\"changeBilling('".$results['id']."', this.value)\"
But my this.value isn't working properly. When I look it up in the page, the id is filling properly, but the second value still says this.value, and not the value of the button that is calling the function.
I see this in the browser element manager:
onclick="changeBilling('149', this.value)"
What am I doing wrong?
Radio Button:
<input type='radio' name='billed' value='Yes' onClick=\"changeBilling('".$results['id']."', this.value)\"/> Yes<br /><input type='radio' name='billed' value='No' onClick=\"changebilling('".$results['id']."', this.value)\" checked/> No
Ideal Output:
onclick="changeBilling('149', 'Yes')"
this.value will be evaluate by the client, as its javascrpt, so you will not see the "baked" value in there from your php, you need to check this at runtime, your best bet is to alert the value of this.value and check if it is what you expect, because this is what will be passed to changeBilling
onClick=\"alert('The value is: '+this.value)\"
the issue you may be getting is that this may not be evaluating to the thing you expect it to, to find out (if you have a good debugger like Element inspector (chrome) or firebug (firefox)) will be to put:
onClick=\"console.log(this)\"
Why don't you just output the literal value in your PHP:
echo "<input type='radio' name='billed' value='Yes' onClick=\"changeBilling('".$results['id']."', 'Yes')\"/> Yes<br /><input type='radio' name='billed' value='No' onClick=\"changebilling('".$results['id']."', 'No')\" checked/> No";
EDIT
I'm not entirely sure what you're expecting to happen, but there does not appear to be anything wrong with the code you have written. To demonstrate this point, see this JSFiddle which shows how this.value works (note that this has got nothing to do with PHP).
Related
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.
This is my form, that is dynamically generated:
<form action='index.php?direction=saveUserPreferences' method='post'>
<label>Office space<input type='checkbox' name='userInterest[]' value='1' id='1'></label>
<label>Grants<input type='checkbox' name='userInterest[]' value='2' id='2' checked></label>
<label>Loans<input type='checkbox' name='userInterest[]' value='3' id='3'></label>
<label>Events<input type='checkbox' name='userInterest[]' value='4' id='4' checked></label>
<label>Connecting<input type='checkbox' name='userInterest[]' value='5' id='5' checked></label>
<label>Office administration<input type='checkbox' name='userInterest[]' value='6' id='6' checked></label>
<label>TAX<input type='checkbox' name='userInterest[]' value='7' id='7' checked></label>
<label>Self employment<input type='checkbox' name='userInterest[]' value='8' id='8' checked></label>
<label>Start up<input type='checkbox' name='userInterest[]' value='9' id='9' checked></label>
<label>Banks<input type='checkbox' name='userInterest[]' value='10' id='10' checked></label>
<input type='text' name='name' id='name'/>
<input type='submit' name='sendData' value='Save'/>
Form points to controller function:
public function saveUserPreferences() {
var_dump($_POST);
}
I tried checking whether post is set etc, and i cannot get array from the post.
I have also login form, that points to the same controller and another function, and it works fine. So i know controller file can and do receive post values.
But for some strange reason, for this particular form i cannot get any values. I have looked up another questions and answers, but cannot get this thing to work.I am also familiar with:
PHP tutorial about the forms
Following that solution i get unidentified index error. Is there something i miss?
Thanks in advance for any tips and tricks ;)
08 03 2015
Hey once again. I have no idea if that helps someone by i would like to give an update of what is happening. As you know, i would get nothing from the page, that is when i would echo values in php file, there would be nothing. Yet, in the firebug and chrome developer tools (in the headers and response part of network section) i was able to see actual response. So what i did is this: created a demo table in database, and tried to insert values that i should see from the form into database. And weirdly enough, it did worked. So, don't ask me how is it possible, since i have no idea, yet. So i decided to work based on the responses from developer tools and firebug. And current path of my data is this. View->Controller->Model (post data to array variable) -> dao class->database. Ok, thanks all for viewing and commenting on that one. Thanks
Is there a redirect before reaching the controller? POST data does only exist for the next request so if you submit to /redirect.php that takes you to /controller.php, $_POST would be empty.
Also your checkboxes doesn't seem right to me. A normal checkbox is like this:
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
So the name is what the checkbox "is about", and the value it's own unique value. The [] is only for select boxes.
I have made a few radio buttons in my php code
echo "<input type='radio' name='rad' value='a'>";
echo "<input type='radio' name='rad' value='b'>";
and my html code is
<form method="post" action="fetch_page_4.php">
<input type="submit" name="submit1" value="Positive" id="btn1"/>
<input type="submit" name="submit2" value="Negative" id="btn2"/></form>
Whenever i click the Positive or Negative button it should go into the respective part in PHP...which it does. But the problem is that i need to find out which of my radio buttons is checked and take the action accordingly.
But i am unable to do so. My code for it is
if(isset($_POST['submit1']))
{
$main_key=0;
if(isset($_POST['rad']) && ($_POST['rad'])=='a')
{
$sub_arr=explode(" ",$array_subject[0]);
$count_sub=count($sub_arr);
$body_arr=explode(" ",$array_message[0]);
$count_msg=count($body_arr);
$main_key=1;
}
}
If i echo the value of "main_key" after this...it is coming out to be 0, thus indicating that it is not going inside the loop even when the radio button is checked and has the required value.
Can somebody please tell me why is this happening?
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.
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.