This is the code for the php file containing the form with radio buttons:
<?php
while($r7 = mysql_fetch_array($rt3)){
$name=$r7["name"];
$s=$r7["question"];
echo "
<div>$s</div>
<form name='Tester' method='post' onSubmit='return RegValidate(this);' action='quest.php' >
<input type='radio' name='w1' value='1'>1<br>
<input type='radio' name='w1' value='2'>2<br>
<input type='radio' name='w1' value='3'>3<br>
";
}
echo" <input name='Submit' value='Tester' type='submit' />
</form>";
?>
The $name and $q are derived from the database using a mysql query. This works fine.
I have used the following javascript code so far:
function RegValidate(Tester)
{
if($('#w1').not(':checked'))
{
alert('Radio not checked');
return false;
}
}
But with this code, I continue to get the error message, and I am not able to move on even though I have selected a radio button.
The while loop produces several sets of radio buttons, as $q, gets the question from the database and posts it on the page along with 3 radio buttons. Each question $q, has the 3 radio buttons in the above, hence the reason for the $name.
How do I validate this form with javascript. NB. The form will only contain radio buttons.
function RegValidate(Tester) {
if ($(":radio[name=w1]:checked").toArray().length == 0) {
alert('nothing selected');
return false;
}
}
FIDDLE
you have an error, your input doesn't have ID or CLASS, so add to all of the input radio class='w1' and try the next code:
if ($(".w1 option:selected").length<1)
alert ('nothing selected');
also take a look to this post: Check if option is selected with jQuery, if not select a default
do these things:
<input type='radio' class='w1' name='w1' value='1'>1<br>
<input type='radio' class='w1' name='w1' value='2'>2<br>
<input type='radio' class='w1' name='w1' value='3'>3<br>
And in js:
Latest edit:
if ( $(':radio[class=w1]').attr('checked') != 'checked' ) {
alert('Radio not checked');
}
Related
I have some PHP code that generates and edits forms of users. The number of forms depend on number of users registered. As a developer, I don't know what's the number of users that can register per day.
The code is like this:
for($i=0;$i<$n;$i++)
{
echo "<form method='post'>
<input type='text' name='fname'>
<input type='text' name='lname'>
<input type='submit' name='submit' value='save'></form>";
}
This code can repeat with 4 or 5 or ++ users. When i do:
if(isset($_POST['submit']))
{
//code
}
for recovering the value of the two inputs.
How does the PHP know the source of the event? It can make a mistake because all button has the same name? Please help me!
You can give the button different names then:
for($i=0;$i<$n;$i++)
{
echo "<form method='post'>
<input type='text' name='fname'>
<input type='text' name='lname'>
<input type='submit' name='submit".$i."' value='save'></form>";
}
And then loop through the names to see if (and which) button is pressed:
for($i=0;$i<$n;$i++)
{
if(isset($_POST['submit'.$i]))
{
//code
}
}
If in addition you want to distinguish post values of the inputfields you could index their names alike.
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?
and thanks for the great help your community provides.
Here is a question regarding Paypal. The code below creates a buy-now non-hosted button. I would like to be able to do run my geoTestArray.php code, currently simply included, as the user presses the Buy Now button, and not when the button is displayed on the page.
Is this possible at all?
Thanks in advance,
Joe
<?php
include ('geoTestArray.php');
echo "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">
<input type=\"hidden\" name=\"business\" value=\"shop#mysite.com\">
<input type=\"hidden\" name=\"cmd\" value=\"$cmd\">
<input type=\"hidden\" name=\"amount\" value=\"$price\">
<input type=\"hidden\" name=\"shipping\" value=\"$shipping\">
<input type=\"hidden\" name=\"currency_code\" value=\"$currency\">
<input type=\"hidden\" name=\"shipto\" value=\"$shipto\">
<input type=\"hidden\" name=\"item_name\" value=\"$title\">";
if ($cmd == 'cart') {
echo "<input type=\"hidden\" name=\"add\" value=\"1\">";
}
include('./quantities2.php');
echo "<input type=\"hidden\" name=\"lc\" value=\"US\">
<input class=\"submit\" type=\"image\" src=\"$button\" border=\"0\" name=\"submit\" alt=\"$altaction\">
<img alt=\"\" border=\"0\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" width=\"1\" height=\"1\">
</form>";
?>
You can display only a form with one input, for example item_id.
After submitting this form you search for item details, execute your geoTestArray.php in backend and after that you can redirect your users to paypal using header("location: $query");
here's how I would do:
<?php
if( isset($_POST['do_action']) && $_POST['do_action'] != "" ) {
//check for non-empty item_id field
//run your geoTestArray.php script
//get item details from database and build your paypal query
$query = "https://www.paypal.com/cgi-bin/webscr&cmd=_xclick"; // add rest of paypal request fields separated by &
header( "Location: $query" );
}
?>
<form action='' method='post'>
<input type='hidden' name='item_id' value'your_item_id'>
<input type='submit' name='do_action' value='Buy'>
<form>
Hope I helped you! :d
Thanks everyone for comments and solution options. I have eventually chosen to modify an onSubmit javascript I had on my page, by adding:
function InterceptForm(formObj) {
var newshipto = httpGet('geoTestArray.php');
formObj.shipto.value = newshipto;
}
The above code makes use of the httpGet() function posted in this other stackoverflow post:
HTTP GET request in JavaScript?
To complete the puzzle:
The code below is in the html in my page
<script language="javascript">
// During onLoad, loop through all forms and for each form object do something
function InterceptForm(formobj) {
formobj.onsubmit = function ff() {
interceptform(formobj);
};
}
</script>
The code below gathers all forms on the page and runs the above javascript on each (NOTE: you may want to name your forms so that you only run the script on the relevant forms and not on all forms):
<script language="javascript">
function GetForms() {
var formsCollection = document.getElementsByTagName("form");
for(var i=0;i<formsCollection.length;i++)
{
if (formsCollection[i].name != "seecart")
{
InterceptForm(formsCollection[i]);
}
}
return true;
}
</script>
The code below is the onLoad function:
<script language="javascript">
function onLoadFunction () {
GetForms();
return true;
}
</script>
Cheers,
Joe
Hi i am having results and its contains a checkbox as <input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>'>
I have a print out list option in the footer as <input type="button" value="Show Printable List" class="butten" onClick="openPrint()">
Its opening the printable list in new pop up window.
function openPrint() {
window.open("<?= $_SERVER["PHP_SELF"]; ?>?<?=$qstring?>&print=true","","width=1024,height=600,menubar=yes,resizable=yes, scrollbars=yes");
}
I am in need to use the above select checkbox and it should show the selected print list in the pop up ?
I hope it should not be problem any1 to understand it. Any help ?
Javascript Modification:
var s = "";
function changeval(value) {
s = value;
}
function openPrint() {
alert(s);
window.open("<?= $_SERVER["PHP_SELF"]; ?>?<?=$qstring?>&print=true","","width=1024,height=600,menubar=yes,resizable=yes, scrollbars=yes");
}
HTML Modification:
<input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>' onchange="changeval(this.value)" />
What I have done?
Created an Global Variable s.
Attached or Bind onchange event on checkbox and set the value of Variable s according.
Now, You have variable s with value, you just have to use it in any function.
Currently you have defined button onclick event , instead of this set onclick event on
checkbox.
<input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>' onClick="openPrint();">
Hope this will help you to solve your problem.
I have this form which allows the input of any product quantity from 1-10:
<form method='post' action='cart.php'>
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
<input type='submit' value='Update'>
</form>
And another form (button) to display a selection of payment modes:
<form action='cart.php' method='post'>
<input type='hidden' name='next'>
<input type='submit' value='Select Payment Mode'>
</form>
What I want to happen is that when a user did not input anything (1st form), ex. null or 0, I want to display an alert box that says 'Product quantity can't be null or 0'.
Here's my code for that:
if (isset($_POST['next'])) {
if ($_POST['quantitychange']==null || $_POST['quantitychange']==0) {
?>
<script type='text/javascript'>
alert('Product quantity can't be null or 0.');
</script>
<?php
}
else {
echo "
//Payment modes here
";
}
}
The error is that even when a user inputs a quantity bet. 1 to 10, it still displays the alert message. Any help? Thank you.
By the way, the input type "number" only works in Google Chrome browser.
Use a small javascript (or jQuery) function to validate the form before posting it. Have this function throw up the alert if your condition isn't met and then return false. If the condition is met, return true, and it gets submitted.
Edited to add since this might get googled, I'll help a bit with code snippet I have used. The below example is jQuery and was used in production for a web application I made for my employees. document.form.doit.submit(); should be the pure javascript way of submitting the form.
<script type="text/javascript">
function subForm() {
// document.form.doit.submit();
if( test condition passes ) {
$('#save_order').submit();
}
}
</script>
<form id="save_order" action="oms_db.php" method="POST">
<input id="doit" type="button"
value="i am a button" onClick="subForm();">
</form>
I think you have some error in your forms. Instead of the below:
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
you should be using something like this:
<input type='number' name='quantitychange' size='2' min='1' max='10' value="<?php echo $_SESSION["itemsSelected"][$i][1]; ?>">
<input type='hidden' name='ProductID' value="<?php echo $_SESSION["itemsSelected"][$i][0]; ?>">
The value parameters in the hidden input fields needs to be echoed from PHP. What you have now is like the value is simple strings ".$_SESSION["itemsSelected"][$i][0].".
I suggest you use
if(empty($_POST['quantitychange'])) { echo 'yourerror'; }
As it is far cleaner then your script. (http://php.net/manual/en/function.empty.php)
Update:
Also, you can't use two seperate forms like you do, your browser only posts whats between
<form>
</form>
Using only one will fix your problem.