textbox submit
Moderator: General Moderators
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
textbox submit
Hi,
I have a form and inside the form I have a textbox where user can add a voucher number, what I want is when the user add the right number inside the textbox (without submitting the form) the price change and if the user add a wrong number inside the textbox (before submitting the form) echo an error message.
Can I achieve this with php, or do I have to use Jquery, if I have to use Jquery how can I add php variables inside Jquery.
Please can you help me by providing some code as I am new to this, and this will be sooo soooo appreciated.
Many thanks,
Jo
I have a form and inside the form I have a textbox where user can add a voucher number, what I want is when the user add the right number inside the textbox (without submitting the form) the price change and if the user add a wrong number inside the textbox (before submitting the form) echo an error message.
Can I achieve this with php, or do I have to use Jquery, if I have to use Jquery how can I add php variables inside Jquery.
Please can you help me by providing some code as I am new to this, and this will be sooo soooo appreciated.
Many thanks,
Jo
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: textbox submit
You want to use ajax here, php can't handle client side stuff because it's a server side language. Could you clarify the second part of the questionYoussefSiblini wrote:Can I achieve this with php, or do I have to use Jquery, if I have to use Jquery how can I add php variables inside Jquery.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: textbox submit
Thank you, you answered both my questions.
Is their a code sample? I will be really happy
Jo
Is their a code sample? I will be really happy
Jo
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: textbox submit
How will the right number be determined? Tested against a database or stored somewhere like in a variable? I can't give specific code because i am unsure about what you have in mind but have a look at this urlYoussefSiblini wrote:what I want is when the user add the right number inside the textbox (without submitting the form) the price change and if the user add a wrong number inside the textbox (before submitting the form) echo an error message.
http://docs.jquery.com/Post
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: textbox submit
OK I will show you the code I am using, as you will see from the code the user has to insert a voucher number and click the <input type="submit" name="CheckVoucher" value="Add Voucher"> to check if the voucher is working, I want to get rid of this button and make it when the user add a voucher number and take mouse out if it is the right number it will give a message saying that 20% off has been taken, and if it is wrong number the message say wrong number.
HTML:
Code: Select all
if (isset($_POST['CheckVoucher']))
{
$Voucher_TextBox = $_POST['Voucher_TextBox'];
if (!isset($_SESSION["VoucherSession"]))
{
if($Voucher_TextBox == 1234)
{
$discount = 0.20; // 20% discount
$price = number_format($price * (1 - $discount), 2); // be sure to have only 2 numbers after the point like: 65.95
$TotalPrice = number_format($price + $shipping, 2); // Get the total price with shipping after the 20% discount.
$total = str_replace('.', '', $TotalPrice); // get rude of the point
$voucherOutput = '20% has been taken off from the item price';
$_SESSION["VoucherSession"] = "hello" or die();
}
else
{
$voucherOutput =
'
<div id="HSBCBuy_Form_Div">
<div class="Dark_Red_Color">The Voucher number you entered is wrong, please insert a correct voucher number.</div>
<div class="Grey_Color">Make sure that you selected the right country from the countries drop list.</div>
</div>
';
}
}
else
{
$voucherOutput = 'You already used your voucher number';
}
}
Code: Select all
<form name="form" action="this.php" method="post">
<INPUT class="HSBCBuy_TextBox" type="text" name="BillingFirstName" value="Paul">
<input class="Voucher_TextBox" name="Voucher_TextBox" type="text" value="add"/><input type="submit" name="CheckVoucher" value="Add Voucher">
<input type="submit" NAME= "continue" value="Continue" class="Light_Red_Button" onclick="javascript:return validateMyForm();">
</form>
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: textbox submit
This some code that i created; i've modified it a bit to suite your purpose. This is the jQuery code. Note : Add an element with the id #cnc where you want the message to display or change the code below to match you existing code.
And on 'ajax.php' you can have something similar to this
Code: Select all
$(document).ready(function(){
//
$('input.Voucher_TextBox').blur(function(){
//
var term = $('input[name="Voucher_TextBox"]').val();
$.post('ajax.php', {'term': $('input[name="Voucher_TextBox"]').val()},
function(data){
if (term != '') {
// displays the data if term is not empty
$('#cnc').html(data);
}
});
});
});
Code: Select all
<?php
// defined within the jQuery code
$term = $_POST['term'];
if ($term == 1234) {
// do something;
}
else {
// do something
}
?>
You should rethink this perhaps; javascript might not be enabled on the visitor's browser and then the code above won't workYoussefSiblini wrote:I want to get rid of this button
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: textbox submit
Thank you,
This is soo useful, and that is very kind of you to share the code
Jo
This is soo useful, and that is very kind of you to share the code
Jo