-2

hi i am new to all html javascript and php. this is suppose to save the input from a html form to my database... but i have no idea why this doesnt work... the code dosent seem to go into the php section to execute the command... kindly advise thank you

<html>
<script type = "text/javascript">
   function processInputs()
   {
      //get data from html form
      var email_data;
      email_data=document.getElementById('txtInput').value;
      //document.write(email_data);

      <?php
         //connect database
         $con = mysql_connect("localhost","admin","password");
         if (!$con)
           {
           die('Could not connect: ' . mysql_error());
           }

         mysql_select_db("internet", $con);
         $newemail = $_GET['email_data'];
         echo $newemail;
         //query
         mysql_query("INSERT INTO user_data (email) VALUES ($newemail)");

         mysql_close($con);
      ?>
   }
</script>

<FORM action="test.php" method="post">
    <INPUT type="text" value="this is a text" name"txtInput" id="txtInput" />
</FORM>

<button type="button" onclick=processInputs() >click!</button>

menotyou
  • 189
  • 1
  • 1
  • 7
  • It might be useful to know PHP is executed on and by the server and JavaScript is executed on and by the browser. This means if you load this page, the server executes all the PHP code and then sends it to the browser. Take a look at your page source code so you can see the PHP has dissapeared. Also, you're missing quotes (like these ") around your onclick attribute. – Bas van den Heuvel Sep 12 '12 at 07:44
  • I would suggest to forget about JavaScript right now and write a simple html form which submits to a php script to insert to the db - and go from there (probably continuing to forget about javascript for a long time to come). You are currently mixing quite a few things together – AD7six Sep 12 '12 at 07:44
  • possible duplicate of [Call php function from javascript](http://stackoverflow.com/questions/7165395/call-php-function-from-javascript) – Amber Sep 12 '12 at 07:45
  • It's not a duplicate, because he's not asking specifically to call a PHP function in javascript, this person does clearly have absolute no knowledge of what he's doing wrong, and why. – dbf Sep 14 '12 at 00:03

3 Answers3

4

You have multiple flaws:

  1. you cannot "embed" like that a PHP script into a javascript function, for that you should use AJAX - try jQuery. PHP is server side, Javascript is browser based.
  2. You have a MySQL injection hole there, you should escape your $_GET with mysql_real_escape_string()
  3. Stop using mysql_* as those functions are deprecated. Read this article
  4. onclick is invalid, it should be <button type="button" onclick="processInputs()">click!</button>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

If you have a perception that -

On calling the js function on some user actions such as button click/hover, etc would trigger the php code and populate the data,

then YOU ARE WRONG.

PHP is a server side scripting language, and you can't use it this way. The complete page is parsed at the server end and given as response to the browser. To do dynamic updates use Ajax calls that would call a php page and populate the html for you.

I suggest refer the coredogs.com site for better understanding of php flow.

mtk
  • 13,221
  • 16
  • 72
  • 112
-1

your function is not being called !!! Change this

onClick = processInputs()

to

onClick = "processInputs()"

and try for an alert statement to see if function is being called or not

Also you cannot have php script within script tag

WomenInTech
  • 1,141
  • 2
  • 18
  • 25