I am trying to post customer and product details to Razorpay from a form. However, it does not trigger the same results as when I have the same variables in the receiving file. The code should be self explanatory;
The form
<form action="razorpay/index.php" method="POST">
Name: <input type="text" name="cname" required><br>
Phone : <input type="phone" name="cphone" required><br>
Email : <input type="email" name="cemail" required><br>
Address : <textarea cols="50" rows="10" name="caddress" required></textarea>
<input type="hidden" name="gtotal" value="<?php echo $gtotal;?>">
<input type="hidden" name="vcomp" value="<?php echo $vcomp;?>">
<input type="hidden" name="proddescr" value="<?php echo $proddescr;?>">
<input type="submit" name="prodetsub">
The PHP file receiving the posted variables
<?php
// If the below variables are not commented, the script works fine with the values
/*$_POST['prodetsub']="submit";
$_POST['gtotal']=8000;
$_POST['vcomp']="Chalo Bazaar";
$_POST['proddescr']="Product Description";
$_POST['cname']="Customer Name";
$_POST['cemail']="customer#email.com";
$_POST['caddress']="Customer Address, Line 1, Line 2, Line 3";
$_POST['cphone']="9999999999";
*/
///But if I try to receive the variables from the post as below, the variables are not passed to javascript and alerted.
if(isset($_POST['prodetsub'])){?>
<form action="charge.php" method="POST">
<?php
$gtotal=trim($_POST['gtotal']);
$vcomp=trim($_POST['vcomp']);
$proddescr=trim($_POST['proddescr']);
$cname=trim($_POST['cname']);
$cemail=trim($_POST['cemail']);
$caddress=trim($_POST['caddress']);
$cphone=trim($_POST['cphone']);
echo $cname."<br>";
echo $cemail."<br>";
echo $cphone."<br>";
echo $caddress."<br>";
echo $gtotal."<br>";
echo $vcomp."<br>";
echo $proddescr."<br>";
?>
<button id="rzp-button1">Pay</button>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var x=<?php echo $gtotal;?>;
var mn="<?php echo $vcomp;?>";
var d="<?php echo $proddescr;?>";
var cn="<?php echo $cname;?>";
alert(cn);
var cem="<?php echo $cemail;?>";
alert(cem);
var cadd="<?php echo $caddress;?>";
alert(cadd);
var cph="<?php echo $cphone;?>";
alert(cph);
var options = {
"key": "rzp_test_WyK93y9mvps7SN",
"amount": x, // 2000 paise = INR 20
"name": mn,
"description": d,
"image": "../images/logo.png",
"handler": function (response){
alert(response.razorpay_payment_id);
},
"prefill": {
"name": cn,
"email": cem,
"contact":cph
},
"notes": {
"address": cadd
},
"theme": {
"color": "#F37254"
}
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e){
rzp1.open();
e.preventDefault();
}
</script>
<input type="hidden" name="shopping_order_id" value="21">
</form>
<?php } else{echo "Nothing Submitted<br>";}?>
The variables echo perfectly in both cases. But they are not passed to the javascript in the case when it comes from the form. And so it does not trigger the alert in the javascript. However, when I activate the variables commented out at the top and have the values assigned in the same file, it works
I guess it is a small issue I am missing. But I have spent a lot of time trying to figure and have lost. Hope someone can see where I am going wrong.
EDIT :
I have json_encoded the variables now.
The Source Code shows values are properly output as follows
var x="30250";
var mn=""Chalo Bazaar"";
var d=""Unicorn Silver Colour. 4 Year old. In good Condition"";
var cn=""Anit Gopinath"";
alert(cn);
var cem=""anitgopinath#gmail.com"";
alert(cem);
var cadd=""506\/C2, Panchganga,\r\nLok Upvan, Phase I, Pokhran II"";
alert(cadd);
var cph=""9833157945"";
alert(cph);
The Chrome javascript console gives an error
"index.php:8 Uncaught SyntaxError: Unexpected identifier"
I am wondering whether it is the slash in the address that is crashing the javascript
You mention that the alert windows are not working, that usually happens when there is an error in the javascript code. We have to find out what the error is. Open the javascript console in your browser (F12 key in Mozilla Firefox).
Oh, the error are the double double quotes! So, in order to fix it, we can delete the json_encode calls, or we can remove the javascript double quotes.
Did it work? Great. Glad to help you.
Don't do this:
var cem="<?php echo $cemail;?>";
You're directly dumping text from PHP into a Javascript context, and can trivially introduce JS syntax errors, killing the ENTIRE <script> block. Always use json_encode() when you're doing php->js output, e.g
var cem = <?php echo json_encode($cemail); ?>;
Why? Consider what happens if that var contains a ". Your code would generate this:
var cem = ""John Doe <jdoe#example.com"";
^^--- start/stop string
^^^^--- undefined variable
^^^ another undefined variable
^--- less than
^^^^---even more undefined variables.
You are checking for a POST input name that has no value. Suggest checking on something else, like a hidden input.
// form has <input type="submit" name="prodetsub">
// which apparentl causes this to be false
if(isset($_POST['prodetsub'])){?>
Why not use something like
<input type="hidden" name="SUBMIT" value="1" />
// and check for form submit like
if ((isset($_POST['SUBMIT'])) && ($_POST['SUBMIT'] ==1)){
Related
I can't get the value using id. In my code I put like this
view
<input type="text" value="<?php echo $add->class;?>" name="cls_<?php echo $add->id;?>" id="cls_<?php echo $add->id;?>"/>
Here I got A
But I can't get the value using this
script
function Add(id){
var cls = document.getElementById('cls_'+id).value;
alert(cls);
}
First step you must making sure for this variable $add->id;, you can check it in inspect element of your browser. Then as I know the value function of jQuery is .val(). so try this document.getElementById('cls_'+id).val();. Hope it helps you :)
Please try as below, if even then not working then please check - $add varaible data using var_dump($add) or print_r($add).
Please check fiddle - https://jsfiddle.net/sashant9/tm7ukbqe/
php -->
<input type="text" value="<?php echo $add->class;?>" name="<?php echo "cls_".$add->id;?>" id="<?php echo "cls_".$add->id;?>"/>
jquery -->
function Add(id){
var cls = document.getElementById('cls_'+id).value;
alert(cls);
}
I am having trouble recording the following _session variable inside my jquery function. I essentially want to grab the value of the coupon code and store it there.
<input type="text" oninput="calculate()" name="couponCode" id="couponCode" value="" placeholder="Please Enter Coupon Code" required>
<script>
$(function() {
$("#signInButton2").click(function(){
<?php $_SESSION['userCoupon'] = echo "<script> $('#couponCode').val(); </script>"; ?>
});
});
Thanks in advance
Here is a quick and easy using jSON. I will get a lot of flak for using jSON in this manner, but too bad everyone.
This should work just fine...
$(function()
{
$("#signInButton2").click(function()
{
var temp = $('#couponCode').val();
example(temp)
});
});
function example(number)
{
$.get("example_set_session.php",
{
number: number
});
}
in example_set_session.php...
<?php
$example_number = $_GET['number'];
$_SESSION['userCoupon'] = $example;
?>
I want to update an int. in my MySQL Database with php. The int. comes from some Javascript. Here is my code where I get the int.:
<form action="SCD.php" method="POST">
<span name="Int_1" id="Int_`1">0</span><br>
<button type="button" onclick="UpdateData()">Update</button>
Name: <input type="Text" name="Name"></input>
<input type="submit" name="submit" value="Send Data">
</form>
The javascript pasts the int. in the span element when you click on the Updat button. That works perfectly.
Now I want to update this in the database. Here is my code:
<?php
$Int_1= $_POST['Int_1'];
$Name = $_POST['Name'];
if($Name) {
include_once("Includes/Database_conx.php");
$query_name = mysql_query("SELECT * FROM Users WHERE Username='$Name'")
or die(mysql_error());
$numrows = mysql_num_rows($query_name);
if($numrows != 0) {
//User exists
$query_int("UPDATE Users SET Int='$Int_1' WHERE Username='$Name'")
or die(mysql_error());
echo "Data updated";
} else {
echo "THat user does not exist";
}
} else {
echo "Please fill in a name";
}
?>
I get a error on line 15 in the php document. Line 15:
$query_int("UPDATE Users SET Int='$Int_1' WHERE Username='$Name'")
what is wrong with it? (Sorry if I missed anything, this is my first question on Stack Overflow)
$query_int is undefined variable, not function name so you can use it like that.
You probably wanted to use mysql_query() here, but it's also not good. Use PDO.
INT is a reserved word in MySQL, so you should use backticks if you have a column with such name. Your query to the server should be
UPDATE Users SET `Int`='$Int_1' WHERE Username='$Name'
<span name="Int_1" id="Int_1">0</span>
span cannot be send to the server.
you can try input element instead.
This wordpress stuff driving me mad again.
I have an output page which uses a short code to call a function (Stores)... the code of which in part is beneath.
It has a dropdown and a table of data, ..the data being dependant on the selected option of the drop down.
I use javascript to set the hidden input...successfully.
In fact I tried a normal, non hidden input as well...same result,..on server side, with$_POST["txtSelection"] or
$_POST["hdnSelect"]
But when I try get it's value on the php server side code, it is empty,..
How on earth do I retrieve it?
the hidden input is inside the form tag.
<?php
function Stores()
{
global $wpdb;
global $MyPage;
$MyPage = str_replace( '%7E', '~', $_SERVER['REQUEST_URI']);
?>
<form name="frmSB_stores" method="post" action="<?php echo $MyPage ?>">
<input type="hidden" name="hdnSelect" id="hdnSelect" value="">
<input type="text" name="txtSelection" size="19" id="txtSelection" value="">
<script type="text/javascript">
function SetDDLValueOnChange (objDropDown) {
var objHidden = document.getElementById("hdnSelect");
if ( objDropDown.value.length > '0')
{
objHidden.value = objDropDown.value; //.substr(0,1);
//alert(" hdn = " + objHidden.value);
window.location = '<?=$MyPage;?>' ;
}
}
</script>
the dropdown's markup here,..then
<table width='100%' border='0' cellspacing='5' cellpadding='3'>
<?php
$Area = $_POST['txtSelection']; //or $_POST['hdnSelect']
which has zilch in it , even though it is set successfully by jvascript
Why is this such an issue in WordPress,
How do i overcome it.
It's nuts spending a full day on something which should be so trivial (works fine in a normal php situation, os asp or asp.net,..but not in WP.)!
TIA
N
This doesn't submit the form it just tell the browser to goto that page. Hence your value always empty.
window.location = '<?=$MyPage;?>' ;
Replace that line with this instead.
document.forms["frmSB_stores"].submit();
I'm working on a little parsing thing to color objects.
For an example, you could type red:Hi!: and "Hi!" would be red.
This is my not working code:
<script type="text/javascript">
function post()
{
var preview = document.getElementById("preview");
var submit = document.getElementById("post");
var text = submit.value;
<?php str_replace("red:*:",'<i class="red">*</i>',text); ?>
preview.value = text;
}
</script>
You have at least two massive problems here.
You can't str_replace with wildcards like you are (the asterisks you use are just that - the asterisk character, not a placeholder).
Your idea of the page-rendering process is off - you can't just call some PHP code in JavaScript and have it update the page. Any PHP code will be executed and printed when your page is generated on the server - it can't interact with the page like JavaScript can (JS can because it is executed within the browser, but the browser never actually sees your PHP code as you can check by going to View->Source and seeing what you see). You certainly cannot reference a JavaScript variable from PHP.
Two options.
Option 1 - Proper Server-Side
if you want to colour objects on page load based on post, do something like this:
<?php
# If the value was posted
$raw = isset($_POST['userstring']) ? $_POST['userstring'] : "";
# Split it based on ':'
$parsed = explode(':', $raw);
$colorClass = "";
$text = "";
if (count($parsed) >= 2)
{
$colorClass = $parsed[0];
$text = $parsed[1];
}
?>
<form action="" method="post">
<input type="text" name="userstring" value=""/>
<input type="submit" value="Submit" />
</form>
<div id="preview">
<?php if (strlen($text) > 0) { ?>
<i class="<?php echo $colorClass; ?>">
<?php echo $text; ?>
</i>
<?php } ?>
</div>
Option 2 - Proper Client-Side
Include jQuery in your <head> tag to make your life easier. If you really don't want to include jQuery you can still change the jQuery calls to your getElementById etc. (you'll want to replace the html() call with '.innerhtml' I think - just look it up).
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
function post() {
var split = $('#userinput).val().split(separator, limit)
if (split.length >= 2) {
var color = split[0];
var text = split[1];
$('#preview').html('<i class="' + color + '">' + text + '</i>');
}
return false; // Stop form submit
}
</script>
<form action="" method="post" onsubmit="post()">
<input id="userinput" type="text" name="userstring" value=""/>
<input type="submit" value="Submit" />
</form>
<div id="preview">
</div>
</body>
You're mixing server and client side technologies here. The code in the php lock is evaluated once (while still on the server). You're looking for something that will operate entirely on the client side.
This means you need to look into Javascript regular expressions, instead of PHP preg_match type stuff.
http://www.regular-expressions.info/javascriptexample.html
You're looking for this type of thing:
stringObject.replace( regularExpressionVarOrLiteral, replacement );
Josh