I need to put echo as condition of if statement. So, for example:
<html>
...
...
<body>
<form onsubmit"<?php $c=false; if(echo "<script>example();</script>";){ $c=true; } echo "\""; if($c){ echo "action=\"./\"";} method="post">
...
</form>
...
...
<script>
function example()
{
alert("This is only an example");
if(..) return true;
return false;
}
</script>
</body>
</html>
So I need to create a function in JavaScript (this top is only an example, but, in realty, it is more complicated) and call it in the if statement.
Update
The code should be execute "action" of the form only if the Javascript function is true.
PHP runs in the server. JavaScript runs in the client. So php can't use a js variable or its function to server side action. What u can do is , u can call JS function from PHP like
echo "<script> myFunction() </script>";
But in the same same case, u can use php values for JS actions,
eg
<script>
var x = <?php echo $myVar ?>
if(x == true){
alert("yahooo");
}
</script>
Related
I am trying to call
this js function that is stored on a file called default.js from a php file.
function myfunction(score) {
console.log("got here");
alert(score);
}
so this is the call for the js function on my php code but it does not work.
<!-- myfunction js file ↓ -->
<script type="text/javascript" src="../js/default.js"></script>
<?php
if ($grade=="100%"){
// not able to call
echo "myfunction($grade)";
}
else{
echo "alert(\"haha\")";
}
?>
Try this you will get the accurate result
<?php
if ($grade == "100%") {
?>
<script>
myfunction('<?=$grade?>');
</script>
<?php
} else {
?>
<script>
alert('in');
</script>
<?php } ?>
you forgot about the <script> tags
change your echo like this:
echo "<script>myfunction('$grade')</script>";
you need to wrap ' around the $grade because you're passing a string
or put <script> before the opening php tag and </script> after the end of php tag
This is your Code.
Define $grade too in PHP or you get a
<b>Warning</b>: Undefined variable $grade in <b>
<script type="text/javascript" src="../js/default.js"></script>
<script>
<?php
$grade = "10%";
if ($grade=="100%"){
echo "myfunction('$grade')";
}
else{
echo 'alert("haha");';
}
?>
</script>
Dont forget the <script></script> for javascript code inside html.
i want to pass PHP variable to Javascript array.
It is a test code, but in further i want to pass the printed product id's and name's to this ajax function to send it to cart
The code above is working without printing the button with echo.Any better ideas are welcome :)
<html>
<?php
$num=3;
echo "<button onclick='funkt('<?php echo $num ?>');'>cc</button>
";
?>
</html>
<script>
function funkt(x){
var c=x;
alert(c);
}
</script>
You weren't properly escaping the quotes inside the echo statement. Also, HTML attributes use double quotes (onclick="..."), not single quotes.
Try this:
<!DOCTYPE html>
<html>
<head>
<script>
function funkt(x) {
var c = x;
alert(c);
}
</script>
</head>
<body>
<?php
$num = 3;
echo "<button onclick=\"funkt('" . $num . "');\">cc</button>";
?>
</body>
</html>
Better option is to add an input field of type hidden and pass the number/name/ID to it. On submission of form or button click, you can fetch it wtih the field Id and pass the value to ajax.
<input type="hidden" id="number" value="<?php echo $num;?>" />
<script>
function funkt(x) {
var c = document.querySelector("#number").value;
alert(c);
}
</script>
You can try to declare variable inside "script" tag, and then use this variable in the javascript afterwards like this:
<?php $num = 5; ?>
<script>
var link_label = '<?php echo $num ?>';
//use your link_label value to load it in ajax here or in other files that has been loaded after this script
</script>
I want to call php function while call that function I need to pass parameter, when click on some text, It has to call the method called test();
How to do this.
I don't know anything about ajax.I have taken this code to try it.To try I have used simple code it is also not working.
This is Ajax.html file
<!DOCTYPE html>
<html>
<head>
<body>
<p>Click Me</p>
<script>
$(document).ready(function() {
$("p").click(function(){
$.ajax({
url:"script.php", //the page containing php script
type: "POST", //request type
success:function(result){
alert(result);
}
});
});
})
</script>
</body>
</head>
</html>
script.php contains,
<?php
function test(){
echo "Hello";
}
?>
how to call php function when user click on some text.where I have to call that test() method. Can Any one help me?
Either you need to call when page called
<?php
test()//mind it
function test(){
echo "Hello";
}
?>
Or use any param like name of action
<?php
if(isset($_GET['action']) && $_GET['action']==1){
test()//mind it
}else{
otherFunc()//mind it
}
function test(){
echo "Hello";
}
?>
Set action in url
url:"script.php?action=1",
I already have a php validation and form setup with PHP. So if someone forgets a username, the validation will add to the errors array and display it on the page when submitted. Now, instead of displaying some text for the error (Username can't be blank) I just want the input box highlighted in red, which I know needs JavaScript. I am having some trouble running this JavaScript function properly. Below is the code.
JavaScript:
<script type="text/javascript">
function myFunction() {
document.getElementById("usernameformitem").className = "formerror";
}
</script>
PHP:
if (isset($_POST['submit'])) {
$required_fields = array("username");
function validate_presences($required_fields) {
global $errors;
foreach($required_fields as $field) {
//the $value is now the un/pw without whitespaces
$value = trim($_POST[$field]);
//if the value does not exist/is blank
if (!has_presence($value)) {
//remember field is really un or pw
$errors[$field] = fieldname_as_text($field) . " can't be blank";
echo "<script>";
echo "myFunction();";
echo "</script>";
}
}
}
validate_presences($required_fields);
HTML:
<form action="new_user4.php" method="post">
<div id="usernameformitem">
<p>Username:
<input type="text" name="username" value="" />
</p>
</div>
<input type="submit" name="submit" value="Create User" />
</form>
Thanks in advance!
make sure the div exists before calling the function, for example echo the script after the div or use
window.onload=function() { if (myFunction) myFunction(); }
FIDDLE
But why even submit the form?
window.onload=function() {
document.getElementById("form1").onsubmit=function() {
if (this.username.value="") {
myFunction();
return false; // stop submission
}
return true;
}
<?PHP if (!has_presence($value)) { .... ?>
if (myFunction) myFunction();
<?PHP } ?>
}
PS: NEVER call a form element name="submit" - it will block you from ever submitting by script
Right under the if submit area of php, I echoed
echo "<script type=\"text/javascript\">";
echo "function myFunction() {";
echo "document.getElementById(\"usernameformitem\").className = \"formerror2\";";
echo "}";
echo "</script>";
Then, in the HTML under the div that I am trying to affect, I entered:
<script>
window.onload=function() { if (myFunction) myFunction(); }
</script>
Only problem I ran into was that style had to be internally set. I'm not sure how to have it pull from my external css yet.
Big thanks to mplungjan!
I want to know if my php variable does not exists, then I want to execute my javascript.
For example-
<?php
if(!isset($_REQUEST['myVar'])){
?>
<script>
alert('variable not exist');
</script>
<?php
}
?>
Is this right way to use javascript code in php extension file
I know all other answers solve your issue but i prefer it to do this way
<script>
<?php
$isset = !isset($_POST['myVar']) ? 'true' : 'false';
echo "var isset = $isset;";
?>
if(isset) {
alert('variable not exist');
}
</script>
when php render your page it will give this output
<script>
var isset = true;
if(isset) {
alert('variable not exist');
}
</script>
Do it like this and it will work:
if (!isset($_REQUEST['myVar'])) {
echo "<script>alert('variable not exist');</script>";
}
you can try writing this piece of code where you want the script to be placed:
<?php
if (!isset($_REQUEST['myVar'])) {
echo '<script>
alert("variable not exist");
</script>';
}
?>