1

I have a webpage which contains an array generated with JavaScript/jquery. On a button press, I want to run a PHP function, which updates a MySQL database with the JavaScript array.

I have a .php file with a PHP function that connects to the database and runs an UPDATE query, I want to use the array with that.

So I have home.php, which has the button:

<?php
include_once ('submit.php')
?>
    <center><button id="submit" class="button1" >Submit<span></span></button></center>

and the array:

<script>
   selectedItemsArray;
</script>

and I have submit.php, which has the sql UPDATE:

    function submit(){
            $sql = $dbh->prepare("UPDATE pending_trades SET item_list=:itemlist,");
            $sql->bindParam(':itemlist', /*array taken from home.php*/);            

            $sql->execute();
   }

I'll convert the array into a JSON before I put it into the database, but I need to know how to get access to the array with my submit.php file, and how to run the submit() function on the HTML button click.

1
  • JavaScript can't talk directly to PHP as they are chronologically incompatible. JS runs on the client, by which time PHP, which runs on the server, has done its thing and long gone. You need AJAX. Commented Jun 21, 2015 at 15:14

2 Answers 2

1

There are multiple issues here. Most crucially, you seem to be confusing server-side and client-side scripting.

You are including submit.php in home.php, which declares a function submit() on the server-side. Your code never executed this function while on the server-side, and so the server-side output is empty,i.e. <?php include_once ('submit.php');?> evaluates to nothing. What the client-side receives is a HTML file with only the button, the function submit() is never passed to the browser.

Remember: server-side scripts are ALWAYS executed on the server and NEVER passed to the browser. That means you will never see anymore <?php and ?> when the file hits the browser - those PHP codes have long finished.

What you need to find out in order to accomplish what you intend:

  1. Use client-side script (JavaScript) to listen to button clicks.

  2. Use client-side script (JavaScript) to submit the form to server through AJAX.

  3. Use server-side script (PHP) to read the data POST-ed, extract the data into an array.

In effect, you are asking three questions. And they are really straightforward; you can read up yourself.

What I'd do is to suggest an architecture for you:

  • home.php or home.html: contains the button, <link href="home.css"> and <script src="home.js">.
  • home.js: client-side script to listen for button click and submit AJAX to submit.php.
  • home.css: styles for the button and other elements in home.html/home.php.
  • submit.php: server-side script to check for POST variables and the SQL update operation.

Another issue: you are using a deprecated tag <center>. I'd advise you to remove it and layout the button using CSS instead.

Sign up to request clarification or add additional context in comments.

Comments

1

use jquery AJAX.

<button  id = "submit"  class = "button1" > Submit <span></span></button>

your js code

$('#submit').click(function(){$.ajax({
      method: "POST",
      url: "submit.php",
      data: itemlist,
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
});

and your php file. don't include file

$array = json_decode($_POST['itemlist'], true);

Remember your js array itemlist should be json format e.g.

$itemlist = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

1 Comment

Thanks. When I'm doing the js AJAX code, will that run the function inside "submit.php"? If not, how can I add that into that code?

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.