1

I have a problem with jQuery and PHP.

I have a .php file that contains a variable, for example var surname = ""; The problem that I am facing is, I want remotely access it from a .html file and give it a value from another variable in the .html file.

Is there any way to do it?

Thanks.

1 Answer 1

1

Ok, so you said you wanted to get a variable from HTML and send it to the PHP file to change data, that is what the variable 'varfromhtml' is. You just need to give it a value you want to send, you can get the value of a div using jQuery eg

<div id="div">data</div>
var varfromhtml = $.("#div").val();

The variable 'varfromhtml' is now equal to 'h08sahdas', you can mess around with that to get it right for you.

Here is the final script:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
var varfromhtml = $.("#div").val();

$(document).ready(function(){
$.ajax({
    method: "POST",
    action: "myfile.php",
    data: {
        varfromhtml:varfromhtml
    },
    success: function(data){
        $('#mydiv').val(data);
    }
});    
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

You should explain how this works using comments. They may not have prior experience with Ajax.
Hey @Dermot thanks, for the answer but it did not help me as i have no past experience with Ajax, imma search a bit more about that though :)
@Dermot if you could explain what exactly is varfromhtml:varfromhtml i would apreciate it a lot

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.