Heres my current code in php with one parameter which works
<?php
$sid = "012";
echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.')">';
?>
Now im trying to pass two parameters or more, but cant seem to get it to work, here is my attempt:
<?php
$name="abc
$sid = "012";
echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.','.$name.')">';
?>
You need quotes around the parameters or javascript treats them like variables instead of strings.
echo '<input type="button" value="Submit" onclick="changeConfirmed(\''.$sid.'\',\''.$name.'\')">';
If one of your JavaScript function parameters is a string, you'll have to correctly enclose it in quotes:
<?php
$name="abc";
$sid = "012";
echo '<input type="button" value="Submit" onclick="changeConfirmed('.$sid.',\''.$name.'\')">';
?>
You get away with $sid not enclosed in quotes because it looks like a number.
Related
I am working with jquery and PHP,I want to display "buttons" according to database value(dynamic) and want to get button values in jquery, I am using "input type hidden", But right now getting value="1"(static), But I want to get "correct" value (if I select button 2 then 2 value should get in jquery)
Here is my HTML code
<?php $j=$records['start_range'];
$max= $records['end_range'];
for($i=$j;$i<=$max; $i++) { ?>
<button class="btn btn-scale btn-scale-asc-<?php echo $i; ?>">
<?php echo $i; ?>
<input type="hidden" <?php if($records['IsRatingQuestion']=="" || empty($records['IsRatingQuestion'])){ ?>name='ques_<?php echo $records['ques_id']; ?>' <?php } else{ ?>name='rangeR_<?php echo $records['ques_id']; ?>' <?php } ?> id="ratings" value="<?php echo $i; ?>">
</button>
<?php } ?>
Here is the script, Where I am wrong?
<script>
$(document).ready(function(){
$("#next1").click(function(){
var rating =$("#ratings").val();
alert(rating);
});
});
</script>
Before anything else, you need to remove input from inside that button. It's not a valid HTML.
Moreover, as mentioned in the comments, you cannot have the same id for all inputs, so I'd just remove it.
How about something like this:
$(document).ready(function() {
$('.item').each(function() {
var $button = $(this).find('button')
var $input = $(this).find('input')
$button.click(function() {
alert($input.val())
})
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
<button>1</button>
<input type="hidden" value="1">
</div>
<div class="item">
<button>2</button>
<input type="hidden" value="2">
</div>
<div class="item">
<button>3</button>
<input type="hidden" value="3">
</div>
<div class="item">
<button>4</button>
<input type="hidden" value="4">
</div>
As far as I am understanding, you just want to show the value of the dynamic button using jquery. For this you just need to pass the value to the button using data attribute and then you can get the value using jquery.
<?php $j=$records['start_range'];
$max= $records['end_range'];
for($i=$j;$i<=$max; $i++) {
echo '<button class="get-value" data-value="'.$i.'">Button '.$i.'</button>';
} ?>
<input type="text" id="show-value">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('click', '.get-value', function(){
var button_value = $(this).data('value');
$('#show-value').val(button_value)
});
});
</script>
I don't think putting an <input> inside of a <button> is valid html (although it seems to work?). Instead you can just assign those values as attributes of the button itself. Also ids have to be unique, so if you have a bunch of buttons with the same id, that is also invalid. I tend to stay away from ids and just use classes to avoid this.
Also, rather than having a bunch of choppy php, you can build a single string of html and echo it back all at once. By using double qoutes " on the outside of a string, you can use single quotes ' on the inside and drop variables right into the string without escaping or concatenating anything. Although you have to wrap associative arrays in brackets {} for it to parse correctly. PHP strings.
I'm guessing $name isn't a static variable, but without seeing the rest of your code, this is just an example:
<?php
$html = "";
$name = ($records["IsRatingQuestion"] == "" || empty($records["IsRatingQuestion"])) ?
"ques_{$records["ques_id"]}" :
"rangeR_{$records["ques_id"]}";
for($i = $records["start_range"]; $i <= $records["end_range"]; $i++) {
$html .= "<button class='btn btn-scale btn-scale-asc-$i ratings' name='$name'>$i</button>";
}
echo $html;
?>
$('.btn.ratings').on('click', function() {
//$(this).text() = $i
//$(this).attr('name') = $name
});
In my PHP code, I echo a string that contains an input field with a javascript event. In this event, I assign a function where we pass 2 arguments. One is a number an another is a string.
Some this is wrong with my concatenation in function arguments. When I click on the input field I got this error
Uncaught SyntaxError: Unexpected end of input
Here is my code:-
<?php
$id = 6;
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo '<input type="checkbox" data-plugin-iso-switchx onchange="smtp_function('.$id.', "dfgdfg")">';
?>
<script>
function smtp_function(a, b){
alert('ok');
}
</script>
</body>
</html>
There's a problem with the " quotes inside the javascript part. Try this:
echo '<input type="checkbox" data-plugin-iso-switchx onchange="smtp_function('.$id.', \'dfgdfg\')">';
Here I use ' quotes instead of " and I escape them with a backslash.
In case the $id is not numeric it also needs quotes. You can do this to add those quotes:
echo '<input type="checkbox" data-plugin-iso-switchx onchange="smtp_function(\''.$id.'\', \'dfgdfg\')">';
All these quotes can become quite unreadable, so it might be better to use this:
$js = "smtp_function('$id', 'dfgdfg')";
echo '<input type="checkbox" data-plugin-iso-switchx onchange="'.$js.'">';
Note that variables inside double quotes are evaluated.
The problem is that you use twice ".
Try changing the type of quotation marks or using escape characters
I prefer to use ` when it is a text string because it does not interrupt line breaks
The following code works.
<?php
$id = 6;
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo '<input type="checkbox" data-plugin-iso-switchx onchange="smtp_function('.$id.', `dfgdfg`)">';
?>
<script>
function smtp_function(a, b){
alert('ok');
}
</script>
</body>
</html>
Hope this can help you.
I have a button who should call an action with 2 parameters (an integer and a String). I m using Yii2.
<button class="button_answer"
onclick="submitAnswer(
<?php echo $id ?>,
<?php echo '\''.Html::encode($title).'\''?>
);">
Submit your answer
</button>
it is working, but when the parameter title contains a single quote or a double quote, the syntax is broken.
I become something like this:
<button class="button_answer" onclick="submitAnswer(214, 'What's the ...?');">
Post your answer
</button>
I dont know how to solve this.
You need to encode the PHP string for JavaScript. Then you need to encode the JavaScript for HTML.
<?php
$js_string_title = json_encode($title);
$js = "submitAnswer($id, $js_string_title)";
$html_safe_js = htmlspecialchars($js);
?>
<button class="button_answer"
onclick="<?php echo $html_safe_js; ?>">
Submit your answer
</button>
A nicer approach would be to avoid inlining the JS altogether:
<button class="button_answer"
data-id="<?php echo htmlspecialchars($id); ?>"
data-title="<?php echo htmlspecialchars($title); ?>">
Post your answer
</button>
Along with something like:
addEventListener("click", answer_handler);
function answer_handler(event) {
var el = event.target;
if (el.classList.contains("button_answer")) {
submitAnswer(el.dataset.id, el.dataset.title);
}
}
I'm having a bit of trouble working with the quotations for this. So, let's make an example of sending two strings we want concatenated through variables, and then running them through a JavaScript function to concatenate them (I understand that this is very basic in PHP and I know how to do it, I'm just using this example for the sake of simplicity).
JavaScript Code:
function con(first, last) {
answer = first+last;
alert(answer);
return;
}
HTML and PHP:
<?php
$first = "Butter";
$last = "Last";
echo '<input type="button" value="Get Answer" onclick="con(".$first.", ".$last.")" />';
?>
This code above does not work, how can I make it work?
Thanks all
If you have a look at the html that that's generating it will be something like this:
<input type="button" value="Get Answer" onclick="con(".$first.", ".$last.")" />
Which as you can see is not correct.
There are a couple of issues with your code, first of all, variables names, like $first won't get evaluated to their value if they are inside single quotes.
Try:
echo '<input type="button" value="Get Answer" onclick="con("'.$first.'", "'.$last.'")" />';
This will output :
<input type="button" value="Get Answer" onclick="con("Butter", "Last")" />
which is still not correct, as you're not passing the arguments to your javascript function correctly.
Try:
echo '<input type="button" value="Get Answer" onclick="con(\''.$first.'\', \''.$last.'\')" />';
which should output
<input type="button" value="Get Answer" onclick="con('Butter', 'Last')" />
that hopefully works :)
Here is your solution
JavaScript Code:
function con(first, last) {
answer= first+last;
alert(answer);
return;}
PHP and HTML code:
<?php
$first = "Butter";
$last = "Last";
echo '<input type="button" value="Get Answer" onclick=con("'.$first.'","'.$last.'") />';?>
when you pass the value to the javascript function through php you must pass the value in 'single quote' or "double quote" like onclick=con("'.$first.'","'.$last.'");
I have a php page which gets $page and $user using post method, I also have a button that i want, to open a URL in the same window using $page and $user variables when clicked, to use them with $_GET[] function.
i want my URL be like:
http://www.test.com/test.php?page=$page&user=$user
my code is like this:
<?php
$page=$POST_['page'];
$user=$POST_['user'];
<?
<html>
<head>
function openurl() {
var user=<?php echo "$user";?>;
var page=<?php echo "$page";?>;
open('www.test.com/test.php?page='+page'&user='+user,'_self');
}
</head>
<body>
<button onclick="openurl()" type="button">open url</button>
</body>
</html>
There is no need for scripting at all
If you want GET:
<?php
$page=$GET_['page']; // should be sanitized and you can use REQUEST for either
$user=$GET_['user'];
$parm = "page=".$page."&user=".$user;
?>
Open URL
If you need to post:
<form action="test.php" method="post">
<input type="hidden" name="page" value="<?php echo $page; ?>"/>
<input type="hidden" name="user" value="<?php echo $user; ?>"/>
<input type="submit" value="Open URL" />
</form>
Change these lines:
<?php
$page=$POST_['page'];
$user=$POST_['user'];
<?
....
var user=<?php echo "$user";?>;
var page=<?php echo "$page";?>;
open('www.test.com/test.php?page='+page'&user='+user,'_self');
to this:
<?php
$page=$_POST['page']; //incorrect $_POST declaration
$user=$_POST['user']; //incorrect $_POST declaration
?> //php tag incorrectly closed
....
var user=<?php echo $user;?>; //echoing a variable not string (no need for quotes)
var page=<?php echo $page;?>; // echoing a variable not string (no need for quotes)
open('www.test.com/test.php?page='+page+'&user='+user,'_self'); // link was broken, forget to put '+' after page variable in link.
You can create a form and via Javascript just run YOURFORMNAME.submit();
Use href in javaScript to move to another location:
location.href="www.test.com/test.php?page='+page'&user='+user"