I have the following stripe payment form on my page:
stripe.createToken(card,{name: '<?php echo $order->customer['firstname'] . ' ' . $order->customer['lastname']; ?>', address_line1 : '<?php echo $order->customer['street_address']; ?>', address_city : '<?php echo $order->customer['city']; ?>', address_state : '<?php echo $order->customer['state']; ?>', address_country : '<?php echo $order->customer['country']['title']; ?>' }).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server
stripeTokenHandler(form, result.token);
}
});
});
I recenetly found out that a customer with the last name o'brian was not being shown the portion of the form that allows them to enter the credit card info. It took a while to find out why they were having that issue.
In any case, should I just be removing the apostrophe with a str_replace? That seems a bit strange to have to remove the apostrophe? Or is there something else I should be doing?
The apostrophe matches the single quote that starts the JavaScript string, and ends the string, causing unmatched quotes in the JavaScript.
Use json_encode() to safely translate a PHP value into the equivalent JavaScript literal, rather than echoing the raw value inside quotes.
stripe.createToken(card,{
name: <?php echo json_encode($order->customer['firstname'] . ' ' . $order->customer['lastname']); ?>,
address_line1 : <?php echo json_encode($order->customer['street_address']); ?>,
address_city : <?php echo json_encode($order->customer['city']); ?>,
address_state : <?php echo json_encode($order->customer['state']); ?>,
address_country : <?php echo json_encode($order->customer['country']['title']); ?>
}).then(function(result) {
Isn't it enough to use double quotes instead of apostrophes to enclose strings?
Something like:
stripe.createToken(card,{name: "<?php echo $order->customer["firstname"] . ' ' . $order->customer["lastname"]; ?>"
Related
I am trying to work on a module where there is an option to show the JavaScript alert and then it should redirect to a php $url. My script is attached below.
$url1=$_SERVER['HTTP_REFERER'];
$url = preg_replace('/\?.*/', '', $url1);
echo "<script type='text/javascript'>alert('Quote Emailed Successfully.');
url = '<?php echo $url; ?>';
window.location='url';
</script>";
It is showing the alert but it's not redirecting.
url = '<?php echo $url; ?>';
You can't nest <?php ... ?> blocks.
Just use the variable. You are in a double quoted PHP string literal, so it will be interpolated.
url = '$url';
window.location='url';
You are trying to redirect to the URL url instead of the value of the url variable.
Remove the quotes.
window.location = url;
Try this
$url1=$_SERVER['HTTP_REFERER'];
$url = preg_replace('/\?.*/', '', $url1);
echo "<script type='text/javascript'>alert('Quote Emailed Successfully.');var url = '" . $url; . "';window.location=url;</script>";
or you can also do it like
echo "<script type='text/javascript'>alert('Quote Emailed Successfully.');
window.location='" . $url; . "';</script>";
You are setting window.location to the value 'url'. Notice the quotes. You should use the declared variable url. Just remove the quotes, as shown below.
$url1=$_SERVER['HTTP_REFERER'];
$url = preg_replace('/\?.*/', '', $url1);
echo "<script type='text/javascript'>alert('Quote Emailed Successfully.');
var url = '<?php echo $url; ?>';
window.location= url;
</script>";
you should use location.href to redirect to php url e.g
alert("alert your text ");
location.href = <?php echo $url_name; ?> ;
check this
<script>
$url1=$_SERVER['HTTP_REFERER'];
$url = preg_replace('/\?.*/', '', $url1);
echo "<script type='text/javascript'>alert('Quote Emailed
Successfully.')";
url = '<?php echo $url; ?>';
window.location='url';
</script>";
If you doesn't need to confirm the redirection then using this script might help
<script>
alert('Your message');
setTimeout(locate,3000);
function locate()
{
window.location.replace('PHP URL');
}
</script>
If you need to confirm the redirection
if (confirm('Go to PHP URL'))
{
window.location.replace('PHP URL'); }
else
{
/* Do Something Else*/
}
I was working on project in which I have to get some text from the database and display it in the text area. Working fine with no line breaks and tabs. but when a line breaks occurs it gives the error
Uncaught SyntaxError: Invalid or unexpected token
My java script code is as :
<script type="application/javascript">
$(document).ready(function () {
document.getElementById('job_type').value = "<?php echo set_value('job_type') ?>";
document.getElementById('job_cat').value = "<?php echo set_value('job_cat') ?>";
if("<?php if(validation_errors() == false) echo "false"; else echo "true"; ?>"=="false")
{
$('#job_title').val("<?php echo $job[0]->job_title ?>");
$('#job_type').val("<?php echo $job[0]->job_type ?>");
$('#job_cat').val("<?php echo $job[0]->job_cat ?>");
$('#salary').val("<?php echo $job[0]->salery ?>");
$('#job_descp').text("<?php echo $job[0]->job_desc ?>");//error here
}
});
});
</script>
The text it was trying to set was
Error details are shown in images
Debug info
I have tried using .html() and .val() too but nothing worked. How can I handle it.
This has already been answered here https://stackoverflow.com/a/4270709/3004335
However, you can use json_encode
<script type="application/javascript">
$(document).ready(function () {
document.getElementById('job_type').value = "<?php echo set_value('job_type') ?>";
document.getElementById('job_cat').value = "<?php echo set_value('job_cat') ?>";
if("<?php if(validation_errors() == false) echo "false"; else echo "true"; ?>"=="false")
{
$('#job_title').val(<?php echo json_encode($job[0]->job_title); ?>);
$('#job_type').val(<?php echo json_encode($job[0]->job_type); ?>);
$('#job_cat').val(<?php echo json_encode($job[0]->job_cat); ?>);
$('#salary').val(<?php echo json_encode($job[0]->salery); ?>);
$('#job_descp').text(<?php echo json_encode($job[0]->job_desc); ?>);//error here
}
});
});
</script>
You do however need to be careful about cross site scripting
See here https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet in particular rule #0
Remove line break(CR LF) in job[0]->job_desc
like this:
$('#job_descp').text("<?php echo str_replace (array("\r\n", "\n", "\r"), '<br>', $job[0]->job_desc);?>");
Basically I used these codes to transform:
echo "<script>";
echo " var img_array=new Array();";
foreach($img_arr as $img_url){
$url=(string)$img_url;
echo "img_array.push('".$url."');";
}
echo "console.log(img_array);";
echo "</script>";
However, errors occur(firefox debug window) :
Error: unterminated string literal
Source File: http://127.0.0.1/CubeCart/index.php?_a=account
Line: 1, Column: 42
Source Code:
var img_array=new Array();img_array.push('http://gtms01.alicdn.com/tps/i1/T1mL3LFhhhXXaCwpjX.png
but after I checked the souce file of the html page, the script is shown like this:
<script> var img_array=new Array();img_array.push('http://gtms01.alicdn.com/tps/i1/T1mL3LFhhhXXaCwpjX.png
');img_array.push('http://gtms01.alicdn.com/tps/i1/T1DQtTFsdFXXaCwpjX.png');console.log(img_array);</script>
With which I don't see anything is wrong.
$jsArray = json_encode ($phpArray);
There is a newline in one of the items in your your php array. Use this:
echo "<script>";
echo " var img_array=new Array();";
foreach($img_arr as $img_url){
$url=(string)$img_url;
echo "img_array.push('". htmlentities(trim($url))."');";
}
echo "console.log(img_array);";
echo "</script>";
I can not successfully get the string using the jquery text()
I have my HTML
<span>/</span>
<span onclick="
var inner_text=$.trim($(this).text());
if(inner_text=='Send Request'){
var post_id=<?php echo $blog_id ;?>;
var event_date='<?php echo $blog_feeds['valid_date'] ?>';
var post_text='<?php echo addslashes($blog_feeds['post_text']) ?>'; //if the string contains ' , then would have error
var request_text_id='<?php echo $blog_id.'comment_text';?>';
var request_content=$('#'+request_text_id).val();
var who_post='<?php echo $blog_feeds['username'] ;?>';
$.ajax({
url:'php/join_request.php',
method:'post',
data:{request_content:request_content,post_id:post_id,who_post:who_post,event_date:event_date,post_text:post_text},
success:function(data){
if(data=='ok'){
$('<?php echo '#'.$blog_id.'request_w' ?>').html('You have sent '+who_post+' a joining reuqest on this event');
$('<?php echo '#'.$blog_id.'request_button'; ?>').html('Request Sent');
$('<?php echo '#'.$blog_id.'request_button'; ?>').css('opacity','0.7');
$('<?php echo '#'.$blog_id.'request_button'; ?>').css('cursor','initial');
$('<?php echo '#'.$blog_id.'comment_text';?>').val('');
}
}
})
}
" id="<?php echo $blog_id.'request_button'; ?>" class="j_request_button">
<?php
$select_request=mysql_query("SELECT `id` FROM `join_request` WHERE `post_id`='$blog_id' AND `who_request`='$username'");
if(mysql_num_rows($select_request)>=1){
echo '<span style="opacity:0.7;cursor:initial">Request Sent</span>';
}
else{
echo '<span style="cursor:pointer">Send Request</span>';
}
?>
</span>
if I add a alert(inner_text) right before if statement, I can successfully alert "sent request",but I can not alert ok within the if statement
Try:
var inner_text=$.trim($(this).text());
This will remove any surrounding whitespace, which is likely if you have line breaks between your spans.
Don't put a full script like that in your HTML. Give your span an id, and use jQuery in a separate script tag:
<span id="sent_request">sent request</span>
<script>
$('#sent_request').click(function(){
var inner_text=$(this).text();
if(inner_text=='sent request'){
alert('ok'); //not working
}
});
</script>
I am using jPlayer to display some videos and I create the playlist on the fly in a php foreach statement:
var playlistacting =
[
<?php foreach($this->result as $val){?>
{
title: '<?php echo $val->getTitle();?>',
artist: '<?php echo $val->getDes();?>',
poster: "<?php echo $val->getVideoId();?>.jpg",
thumb: "<?php echo $val->getVideoId();?>.jpg",
flv: "<?php echo $val->getVideoId();?>.flv",
},
<?php }?>
];
and $val->getDes() example would be I have a one-hour (approximately) solo musical revue called "Manhattan With A Twist". The entire...
the error I get is
unterminated string literal
[Break On This Error]
artist: 'I have a one-hour (approximately)solo musical revue called "Manhattan W...
jquery.js (line 2, col 32)
and is poining to the ' at the beginning of the string.
I can do this: title: "<?php echo htmlspecialchars($val->getTitle());?>",
and I will get the same error but with " instead of '.
I'm not sure what is happening here. Is it complaining about that ' not being escaped or what?
Any ideas?
simply use json_encode:
var playlistacting = <?php echo json_encode($this->result);?>;