1

I am not very experienced in web programming and am attempting to run a script which updates my database.

            <script>
            function myFunction() {
                var texts = document.getElementById("content").textContent;
                alert(texts)
                <?php
                    include_once 'accounts/config.php';
                    $text = ...;
                    $tbl_name='enemies'; // Table name 
                    $query = "UPDATE enemies SET text=('$text') WHERE id=1";
                    $result = mysql_query($query) or die(mysql_error());
                ?>
            }
            </script>

I have no idea what to put in the $text section as shown with $text = ...; in order to get the variable texts from above.

EDIT

I have updated my code but the function does not seem to be accessing the PHP file. I am using a button to call the function and I have also tested it so i know the function is being called. My file is called update.php and is in the same directory as this file.

            <button onclick="myFunction()">Click This</button>

        <script>
        function myFunction() {
            var texts = document.getElementById("content").textContent;
            $.ajax({
                url: "update.php",
                type: "POST",
                data: {texts:texts},
                success: function(response){
                }
            });
        }
        </script>
  • make an XMLHtttpRequest, the one you have there doesn't work that way. the PHP script already ran even before you invoked the function – Kevin Apr 18 '15 at 08:08
  • You could try using jquery ajax to submit the value of text to a php file – Harigovind R Apr 18 '15 at 08:13
  • You will want to use an ajax method to do something like this. Have a look at [jQuery](http://api.jquery.com/jquery.ajax/) they have an ajax method that will allow you to do this. – TheSk8rJesus Apr 18 '15 at 08:13
  • php runs on the server, javascript at the client. You can not combine the two languages in the way you did, you need Ajax. – ByteHamster Apr 18 '15 at 08:14

4 Answers4

1

you can post your $texts value to other php page using ajax and get the variable on php page using $_POST['texts'] and place update query there and enjoy....

function myFunction() {
                    var texts = document.getElementById("content").textContent;

    $.ajax({
            url: 'update.php',
            type: "POST",
            data: {texts:texts},
            success: function(response)
            {


            }
        });

And your php file will be named as update.php

<?php
                include_once 'accounts/config.php';
                $text =$_POST['texts'];
                $tbl_name='enemies'; // Table name 
                $query = "UPDATE `enemies` SET `text`='".$text."' WHERE `id`=1";
                $result = mysql_query($query) or die(mysql_error());
            ?>
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
0

You are wrong in approach

You should use ajax to post 'texts' value to your php script https://api.jquery.com/jquery.post/ and create separate php file where you will get data from ajax post and update DB

javascript:

<script>
    function myFunction() {
       var texts = document.getElementById("content").textContent;

       $.ajax({
          type: "POST",
          url: "update.php",
          data: "texsts=" + texts,
          success: success
        });
        }
</script>

update.php

 <?php
                    include_once 'accounts/config.php';
                    $text = $_POST['texts'];
                    $tbl_name='enemies'; // Table name 
                    $query = "UPDATE enemies SET text=('$text') WHERE id=1";
                    $result = mysql_query($query) or die(mysql_error());
                ?>
Evgeniy Kuzmin
  • 2,384
  • 1
  • 19
  • 24
0

PHP runs on the server and then generates output which is then returned to the client side. You can't have a JavaScript function make a call to inlined PHP since the PHP runs before the JavaScript is ever delivered to the client side.

Instead, what you'd need to do is have your function make an AJAX request to a server-side PHP script that then extracts the data from the request body and then stores it in the database.

PHP: "/yourPhpScript.php"

<?php 
include_once 'accounts/config.php';
$text = $_POST['data'];
$tbl_name='enemies'; // Table name 
$query = "UPDATE enemies SET text='".$text.'" WHERE id=1";
$result = mysql_query($query) or die(mysql_error());
?>

JavaScript:

function myFunction() {
    var texts = document.getElementById("content").textContent;
    alert(texts);

    // append data as a query string
    var params = 'data='+texts;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        // when server responds, output any response, if applicable
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }

    // replace with the filename of your PHP script that will do the update.
    var url = '/yourPhpScript.php';
    xmlhttp.open("POST", url, true);
    xmlhttp.send(params);
}

A word of caution: This is not a safe, production-friendly way of updating data in your database. This code is open to SQL injection attacks, which is outside the scope of your question. Please see Bobby Tables: A guide to preventing SQL injection if you are writing code that will go into production.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
-2

i will use PDO if i was you, what you do mysql_query are outdated, if you use my framework https://github.com/parisnakitakejser/PinkCowFramework you can do the following code.

<?php
include('config.php');

$text = $_POST['text'];

$query = PinkCow\Database::prepare("UPDATE enemies SET text = :text WHERE id = 1");
$bindparam = array(
    array('text', $text, 'str')
);
PinkCow\Database::exec($query,$bindparam);

$jsonArray = array(
    'status' => 200
);
echo json_encode($jsonArray);
?>

place this code in jsonUpdateEnemies.php file and call it width jQuery

<script>
function myFunction(yourText) {
    $.post( 'jsonUpdateEnemies.php', {
        'text' : yourText
    }, function(data)
    {
        alert('Data updated');
    },'json');
}
</script>

its a little more complex then you ask about, but its how i will resolved your problem, :)

ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66