I am having a bit of troubles submitting my form with jQuery..
I got this:
<div class='widget-body'>
<div class='center' id='formHolder' style='display:none'>
<div id='amountDiv'>$<span id='amountSpan'></span></div>
<div class='pagination pagination-centered margin-none' style='font-size:15px;'>
<ul>
<li><a href='javascript:void(0)' id='dec'>-</a></li>
<li class='active'><a href='javascript:void(0)' class='addfunds'>Add Funds</a></li>
<li><a href='javascript:void(0)' id='inc'>+</a></li>
</ul>
</div><!--//Pagination-->";
$contents .= "
<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\" id=\"ppform\">
<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" />
<input type=\"hidden\" name=\"business\" value=\"{$set['pp_paymentemail']}\" />
<input type=\"hidden\" name=\"quantity\" value=\"1\" />
<input type=\"hidden\" name=\"item_name\" value=\"{$set['rbalanceadd_paymenttext']}\" />
<input type=\"hidden\" name=\"amount\" class='a' value=\"\" />
<input type=\"hidden\" name=\"item_number\" value=\"{$ir['username']}\" />
<input type=\"hidden\" name=\"no_shipping\" value=\"1\" />
<input type=\"hidden\" name=\"return\" value=\"$return\" />
<input type=\"hidden\" name=\"currency_code\" value=\"{$set['currency_type']}\" /></form>
<form method=\"post\" action=\"https://secure.payza.com/checkout\" id='pzform' />
<input type=\"hidden\" name=\"ap_purchasetype\" value=\"service\" />
<input type=\"hidden\" name=\"ap_merchant\" value=\"{$set['ap_paymentemail']}\" />
<input type=\"hidden\" name=\"ap_itemname\" value=\"{$set['rbalanceadd_paymenttext']}\" />
<input type=\"hidden\" name=\"ap_currency\" value=\"{$set['currency_type']}\" />
<input type=\"hidden\" name=\"ap_returnurl\" value=\"{$myi}\" />
<input type=\"hidden\" name=\"ap_quantity\" value=\"1\" />
<input type=\"hidden\" name=\"ap_amount\" class='a' value=\"\" />
<input type=\"hidden\" name=\"ap_description\" value=\"{$ir['username']}\" />
</form>
";
$contents.="
</div><!--//formHolder-->
<div id='btHolder'>
<div class='center'>
<img src='themes/steel/theme/images/icons/paypal.png' style='margin-left:10px;margin-right:10px;' id='paypalBt'>
<img src='themes/steel/theme/images/icons/payza.png' id='payzaBt'>
</div><!--//Center-->
<br />
</div><!--//btHolder-->
</div><!--//Widget Body-->
So, first off all the user is choosing his/her payment method. That is done by either choosing #payzaBt or #paypalBt
After that, the user must specify how much money he/she wishes to add.
That is done with #inc or #dec (This will add the amount to the amount input field of both the PayPal and Payza form.
My jQuery looks like this:
$(document).ready(function() {
$('#paypalBt').click(function() {
$('.addfunds').attr('id','addfundspp');
$('#btHolder').hide();
$('#formHolder').show();
});
$('#payzaBt').click(function() {
$('.addfunds').attr('id','addfundspz');
$('#btHolder').hide();
$('#formHolder').show();
});
$('#addfundspz').click(function() {
$('#pzform').submit();
});
$('#addfundspp').click(function() {
$('#ppform').submit();
});
$('#amountSpan').text('5');
$('.a').val('5');
$('#inc').click(function(){
$('#amountSpan').text( Math.min(100, Number($('#amountSpan').text()) + 5) )
$('.a').val( Math.min(100, Number($('.a').val()) + 5) )
});
$('#dec').click(function(){
$('#amountSpan').text( Math.max(5, Number($('#amountSpan').text()) - 5) )
$('.a').val( Math.max(5, Number($('.a').val()) - 5) )
});
});
Now all of this works fine. The problem is when the user must submit the PayPal or the Payza form. As you can see in the above jQuery code I am first adding an ID attribute to the "Add Funds" button; when that button is clicked, I am using the jQuery submit form function. But this doesn't work. No form is submitted.
What is wrong?
Thanks in advance.
That is because #addfundspz (and #addfundspp) doesn't exist when document is ready
You can use event delegation
$(document).on('click','#addfundspz',function() {
$('#pzform').submit();
});
http://learn.jquery.com/events/event-delegation/
Related
See question -
I've tried this:
echo "<form action='index.php' method='post'>
Use the previous input of participants: <input type='radio' name='participants[]'value='$participants[0]'>
OR <br>
<form action='index.html' method='post'>
Use a different input of participants: <input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'> </form> <br>";
Both of the radio button lead me to index.php when I want to be able to go to index.html in case i press on the second radio button.
You may solve it by using JQuery
<form action='index.php' method='post'>
Use the previous input of participants:
<input type='radio' name='participants[]' value='$participants[0]'>
<br/>OR <br>
Use a different input of participants:
<input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form = $('form');
var input = $('input[type=radio]');
input.on('click', function() {
if ($(this).val()==0) {
form.prop('action', 'index.html');
} else {
form.prop('action', 'index.php');
}
});
});
</script>
i have this code:
Amount<input type='text' name='amount' size='10' >
<br>
Free<input type='text' name='fees' size='10' >
<br>
------------
<br>
<input type='radio' name='amount_type' value='Receive'checked> Receive<br>
<input type='radio' name='amount_type' value='Send'> Send<br>
-------------
<br>
<input type='radio' name='curr' value='LBP'checked> LBP<br>
<input type='radio' name='curr' value='USD'> USD<br>
what i want to do is
if amount more than 5000 , check radio button curr : value LBP else check usd
if fees are inserted , in radio button amount_type : check Receive else check send.
can you assist please
Use this code.
Amount<input type='text' name='amount' size='10' id='amount' >
<br>
Free<input type='text' name='fees' size='10' id='fees' >
<br>
------------
<br>
<input type='radio' id='receive' name='amount_type' value='Receive'checked> Receive<br>
<input type='radio' id='send' name='amount_type' value='Send'> Send<br>
-------------
<br>
<input type='radio' id='lbp' name='curr' value='LBP'checked> LBP<br>
<input type='radio' id='usd' name='curr' value='USD'> USD<br>
jquery
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("#usd").prop("checked", true);
$("#send").prop("checked", true);
$("#amount").on("input", function(){
// Print entered value in a div box
if($("#amount").val() > 5000){
$("#lbp").prop("checked", true);
}else{
$("#usd").prop("checked", true);
}
});
$("#fees").on("input", function(){
// Print entered value in a div box
if($("#fees").val() > 0){
$("#receive").prop("checked", true);
}else{
$("#send").prop("checked", true);
}
});
});
</script>
check out online demo : DEMO
I'm making a Quiz. And with each question I'm showing the possible answers( "True" or "False") with a While loop in PHP:
echo "<form method='post' action='quizCheck.php'>";
while(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer' value='true' />
</label>
<label>False
<input type='radio' name='answer' value='false' />
</label>
</div>";
}
echo "</form>";
Let's say there are 10 questions and I select "True" on 6 questions.
What code do I have to put in quizCheck.php so it can count the number of "True" answers and store it in a variable?
You will need to do two things, first you need a submit button in the form:
<button type="submit" value="Submit">Submit</button>
Then you will also need the names of the radio inputs to be unique so in the while loop (which you really should just change to a for loop) do:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer{$x}' value='true' />
</label>
<label>False
<input type='radio' name='answer{$x}' value='false' />
</label>
</div>";
}
When the form is submitted, then in quizCheck.php you just check $_POST[answer0] through $_POST[answer9] to see which are true and increment a counter.
If you want the answers in a single array then do this:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answers[$x]' value='true' />
</label>
<label>False
<input type='radio' name='answers[$x]' value='false' />
</label>
</div>";
}
When this form is submitted, then in quizCheck.php you just get something like $answers = $_POST[answers] and then go through answers[0] to answers[9] for example
I have a table and use the following to create a button inside one of the cells:
print("<td> <input type=\"submit\" name=\"toedit\" value=\"Submit\" >
<form action=\"Manage_Customer_Information_refined_list.php\" method=\"post\">
<input type=\"hidden\" value=\"Submit\" name =\"submit_button\" >
</form>
</td>");
When i go to call upon the button (press it) using
if(print_r($_POST["\"submit_button\""]))
{
print "button pressed";
}
It says
undefined index "submit_button"
Please Help :/
try this
print("<td>
<form action=\"Manage_Customer_Information_refined_list.php\" method=\"post\">
<input type=\"hidden\" value=\"Submit\" name =\"submit_button\" >
<input type=\"submit\" name=\"toedit\" value=\"Submit\" >
</form>
</td>");
and if you want to submit on same page then remove Manage_Customer_Information_refined_list.php form form.
Change
<input type=\"hidden\" value=\"Submit\" name =\"submit_button\" >
to
<input type=\"submit\" value=\"Submit\" name =\"submit_button\" >
Remove
<input type=\"submit\" name=\"toedit\" value=\"Submit\" >
Change your condition to this
if(isset($_POST["submit_button"]))
{
print "button pressed";
}
The code you write <input type=\"hidden\" value=\"Submit\" name =\"submit_button\" > will be evaluated to ... name="submit_button" which means you should access this input value from the server side using $_POST["submit_button"].
The way you did write $_POST["\"submit_button\""] expect the input field to be written as <input type=\"hidden\" value=\"Submit\" name =\"\"submit_button\"\" > and i don't think that this is a valid syntax, also you should put the submit button inside the form not outside of it.
I want to start a jquery (freeow plugin) when I got an error in e-mail input field form.
I'm checking some fields on the form and each error that I got I want to open an alert message.
I can do this using a button, like this...
<div id="freeow-tr" class="freeow freeow-top-right"></div>
<div id="freeow-br" class="freeow freeow-bottom-right"></div>
<div class="form-line-check">
<input id="freeow-title" value="Freeow!" type="hidden" class="text" />
<input id="freeow-error" value="0" type="hidden" />
<input id="freeow-position" value="#freeow-tr" type="hidden" />
<input id="freeow-style" value="smokey" type="hidden" />
<input id="freeow-message" value="Error message..." type="hidden" />
</div>
<div class="form-line">
<input id="freeow-show" type="button" value="Click to Freeow!" />
</div>
How do this without the button? Inside the PHP code.
if ($erro == "0") {
if(filter_var($email01, FILTER_VALIDATE_EMAIL)){
$erro = "0";
} else {
echo "<div id=\"freeow-tr\" class=\"freeow freeow-top-right\"></div> ";
echo "<div id=\"freeow-br\" class=\"freeow freeow-bottom-right\"></div> ";
echo "<div class=\"form-line-check\"> ";
echo "<input id=\"freeow-title\" value=\"Freeow!\" type=\"hidden\" class=\"text\" /> ";
echo "<input id=\"freeow-error\" value=\"0\" type=\"hidden\" /> ";
echo "<input id=\"freeow-position\" value=\"#freeow-tr\" type=\"hidden\" /> ";
echo "<input id=\"freeow-style\" value=\"smokey\" type=\"hidden\" /> ";
echo "<input id=\"freeow-message\" value=\"Error message...\" type=\"hidden\" /> ";
echo "</div> ";
$erro = "1";
}
}
You have to create a javascript function
function mensagem01() {
var title, message, opts, container;
title = $("#freeow-title").val();
message = $("#freeow-message").val();
message = 'E-mail not in database...';
opts = {};
opts.classes = [$("#freeow-style").val()];
opts.prepend = false;
opts.classes.push("error");
opts.autoHide = false;
opts.classes.push("slide");
opts.hideStyle = { opacity: 0, left: "400px" };
opts.showStyle = { opacity: 1, left: 0 };
container = $("#freeow-position").val();
$(container).freeow(title, message, opts);
}
HTML code need to have the freeow definitions
<div id="freeow-tr" class="freeow freeow-top-right"></div>
<div id="freeow-br" class="freeow freeow-bottom-right"></div>
<div class="form-line-check">
<input id="freeow-title" value="Lembrou?" type="hidden" class="text" />
<input id="freeow-error" value="0" type="hidden" />
<input id="freeow-position" value="#freeow-tr" type="hidden" />
<input id="freeow-style" value="smokey" type="hidden" />
<input id="freeow-message" value="message field must be empty" type="hidden" />
</div>
Inside PHP code
You need to include the javascript code to call the javascript function message
echo "<script type=\"text/javascript\"> $(document).write(mensagem01()); </script> \n";
When the php is executed the javascript write the document, which contains the call to the freeow message.
What you are trying to achieve will is best done with AJAX and some more logic than you have in your code.
You want to catch the form submit with jQuery. Using event.prefentDefault() you can stop the form from submitting. Then, you want to AJAX your FormData to the server.
In your PHP script, you do your validation. If it succeeds, return something (for example, a JSON object) that tells your front end that the validation was a success. If not, you want an object that describes what exactly is failing.
From front end, you check to see if the validation was a success (do whatever), or not (show freeow)