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?
Related
I have the following code which is causing me problem. The code generates two buttons, one to remove the selected project and the other to cancel the deletion of the project. The remove function works well, however I haven't found a way to make my cancel button redirect to my url (when I click on it, nothing happens). Any clue?
...
echo "<form method='post'>";
echo "<input type='hidden' value='".$currentid."' name='project'/>";
echo "<b>".$project_name."</b>";
?>
<div class="btn_2"> <input type="submit" name="save" value="Remove"><input type="submit" onclick="window.location.replace('www.myurl.ca')" value="Cancel"></div>
</form>
You're using a submit button and tagging JS on to it. I'm expecting that you're actually submitting the form, but I could be wrong.
If nothing is happening with your JS, you may try doing window.location.href = "www.myurl.ca"; return false; where including the return false should avoid the submit.
The best solution would probably be to make it a plain button and wrap it in an A tag. You would avoid submitting your form, redirect, and all without requiring JS be active.
...
echo "<form method='post'>";
echo "<input type='hidden' value='".$currentid."' name='project'/>";
echo "<b>".$project_name."</b>";
?>
<div class="btn_2"> <input type="submit" name="save" value="Remove"><button type="button">Cancel</button></div>
</form>
Using the formaction button attribute works.
<input type="submit" name="save" value="Cancel" formaction="http://www.myurl.ca">
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');
}
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.
I'm trying to use AJAX to make my website a little slicker than it currently is.
I have a block of code that shows the following
if($guess == 0){
echo "Enter a guess:;
*AJAX form* ---> inserts a MySQL database record with the users guess
<div to show results of script for form> - not necessarily needed
}else{
echo "Update your guess:";
*SAME AJAX form* ---> updates the MySQL database record with the users new guess
<div to show results of script for form> - - not necessarily needed
}
The problem I'm having is that I want part of the webpage to show:
Your latest guess is: £x.xx
However because a user will start off with 0 guesses, and AJAX sends the form "behind" the scenes, I'm struggling in knowing how to show the above line once the user has made a guess.
So also that way when they revisit the page at a later date, it shows the last guess they had.
Those are the only 2 elements of the page I want to be able to refresh, the rest of the information doesn't have to refresh.
Some more code here:
<?php
if($guessess_open == 1){
echo "<h2>guesses are CLOSED</h2>";
}
else{
//it's this part that I want to have always shown on the page, but when I visit the page fresh it doesn't show me any results.
<div id="results"></div>
<form name="myform" id="myform" action="" method="POST">
<!-- The all important guess field -->
<label for="guess" id="guess_label">Guess<br></label>
<input type="text" name="guess" id="guess" size="10" value=""/>
<?php
echo "<input type='hidden' name='user_id' id='user_id' size='10' value='$user'/>";
echo "<input type='hidden' name='item_id' id='item_id' size='10' value='$itemID'/>";
echo "<input type='hidden' name='title' id='title' size='100' value='$title2'/>";
echo "<input type='hidden' name='owner_id' id='owner_id' size='10' value='$ownerid'/>";
echo "<input type='hidden' name='guesses_open' id='guesses_open' size='10' value='$guesses_open'/>";
echo "<input type='hidden' name='exist' id='exist' size='10' value='$exist'/>";
?>
<!-- The Submit button -->
<br>
<input type="image" name="submit" src="URL" width="150px" height="100px">
</form>
The only way I can get it to show something in that part of the page is to have some code in which checks a database for a guess and if it exists display the result, but then when I submit a new form, because it only updates the results it doesn't update the part of code for example could include
else{
include("URL.php?item_id=" . $itemID. "&user=" .$user. "");?>
//it's this part that I want to have always shown on the page, but when I visit the page fresh it doesn't show me any results.
<div id="results"></div>
which shows the latest guess when a user visits the page, but then when a new guess is made, this doesn't update... I don't have to have my forms php up display the result if the above part of code would update each time I make a guess... I hope this is clearer?
it has error because $_POST['sub1'] can't be accessed
is there any approach or solution to echo the value of $_POST['sub1']? or impossible? no way? even with another arrays?
i had question about my code nobody solved it! then I decide to tell it in simple way.
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
I think I know what is wrong here.
When you submit the form the second time (for sub2) you are no longer posting the value of sub1 along with it, just sub2.
This should fix it:
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='hidden' name='sub1' value='" . htmlentities($_POST['sub1']) . "'>";
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
You're using submit buttons. Only the button that you actually click will have its name/value pair sent to the server. When you click the sub2 button, only sub2=sub2 is sent, so sub1 won't exist in the $_POST array.
followup:
$_POST is created for you by PHP based on what's sent from the browser. The way you've built your form makes it impossible for 'sub1' to exist when you click the 'sub2' button. In other words, you need to use the SAME name= for BOTH buttons, and change the value= as appropriate:
html:
<input type="submit" name="submit" value="sub1" />
<input type="submit" name="submit" value="sub2" />
php:
if (isset($_POST['submit'])) {
echo "You clicked the {$_POST['submit']} button";
}