0

I have a button with onclick function. It passes a variable $x into the java script. Shortly what i want to do is to pass the $x="aaa" to the javascript, and make the function to change this variable into lets say $x="bbb" without refreshing the website. So i would be able to click it all the time and the js could change the value of given variable. I mean the content variable going from button to javascript would be changed depending on js function code. This may sound complicated i am really sorry for that. Any ideas? (the code below i just made up because it would be to long to explain here all my code that is very long now)

<?php x = "aaa";  ?>

<button onClick="someFunction(<?php $x ?>)"></button>

<script> 
function someFunction (x){
//what can i do here to make this "aaa" string in variable change 
//into for example "bbb" on clicking it again?
}
</script>
1

3 Answers 3

1

You can't change server side language value from client script

All you can do is to pass that php value to javascript

Like this

<button onClick="someFunction(<?php echo $x; ?>)"></button>

But you can do like it

Try like this

js

$.ajax({
  method: "GET",
  url: "some.php",
  data: { x: "John" }
})
  .done(function( msg ) {
    <?php x=$_COOKIE["X"]; ?>
});

some.php

$_COOKIE["X"]=$_GET["x"];

EDIT

How I can cut it off from button arguments

HTML

<button id="myButton" onclick="someFunction(<?php $x ?>)"></button>

JS

$("#myButton").on("click",function(){
    var onclick=$(this).attr("onclick");
    var str="Hellow world";
    var someFunction="someFunction('"+str+"')";
    $(this).attr("onclick",someFunction);
});
Sign up to request clarification or add additional context in comments.

11 Comments

and ajax cant do this? its asynchronious thing.
yea you can do it but not direct way. like say you have database in ajax you pass that value to another php . that php file save that in database and another ajax retrive these value from database.
damn thats pretty sad, I was hoping that js got some more power. Cant i Just manipulate the onclick values?
like i said before , you can't directly do that but indirect way you can do that . like instead of database you can use cookies change that cookie value in ajax and after call that you can simply assign cookie value in your x .
But i mean.. i dont need this $x variable. I can cut it off from button arguments. Just want to pass another string into onclick. It is all going on client side right? How to make this $x disapear and replace it with any string?
|
0

With the above code in mind... Not knowing if it is specifically strings that you wish to manipulate with javascript but you can use php / a server side language to generate the variable value based upon whatever calculations you need and then use javascript to play with it on the client side.

<button onClick="someFunction()">Click me now to see what happens to x</button>


<script>
<?php echo "var x = 'a';";  ?>
function someFunction(){
    x=parseInt( x.charCodeAt(0) );
    x++;
    x=String.fromCharCode( x );
    alert(x)
}
</script>

1 Comment

Damn this looks genius. And they say its bad to combine php and js haa!
0

After research and other's help, this works :

//This gets the page's width on the first load
let sWidth = window.screen.width;

//This says : execute "myfunction" when "resize" event occurs
window.addEventListener("resize", myFunction);

//details of myFunction
function myFunction() {
    //first get the page's width again
    let sWidth = window.screen.width;

    //show me the page's width within console which is within inspect
    console.log(sWidth);

    //now this is for differentiating Mobile and Desktop version
    //which you can change the statement by need
    if (sWidth <= 576){
        console.log("Mobile version");
    }else{
        console.log("Desktop version");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.