-1

I'm writing a little online programm that must save a football tournament results. I'm adding a team names using JS, and i want to save them on server site (in text file). For this i need PHP, i think. JS looks like this:

<script>
function addTeam(){
    var name = $("#FormForNameEntering").val();                                    
    if (name != ""){
        $("#TableDataNameContainer").append("<p class='teamName'>" + name + "</p>");
        $('#FormForNameEntering').val("");
    }
}
</script>

So i need to save variable "name" in text file. For this i need to tranfer its value to PHP? How?

2 Answers2

0

You have three options. You would make an Ajax call, form submission of some kind with a submit button and action to be set to a php file, or finally have your whole page in a php file and after receiving data in JavaScript do your php there.

Asheliahut
  • 901
  • 6
  • 11
  • Actually, i am writing my page in index.php file. In this case i need to use get() or post()? – Eduard Lebedev Jan 20 '15 at 08:52
  • You actually would still need to use a form or an Ajax call, the other method I will not recommend. Just in form action set it to index.php and set method to POST. Have in a php section checks on the data isset set them to a variable after pulling them from the $_POST variable. And do your file update and send back a success message or failure. – Asheliahut Jan 20 '15 at 08:57
  • I found my mistake. I used index.php for HTML and JS and PHP. It seams like i cant use JS $.post('index.php ... from the same index.php file. If i use new PHP file (test.php), everything works ... – Eduard Lebedev Jan 21 '15 at 09:13
0

You need to send it to the server! e.g: window.location = www.example.com?variableName='+yourJSVariable And just retrieve it via PHP's $_GET. Other option you have is sending it to the server asynchronously (=in background lets say). Have a look at jQuery's ajax/get/post functions, or play around with XHR obj in js. Or try submiting via html forms.

Dont worry we've all started somewhere!

maht0rz
  • 96
  • 7