Use PHP ONLY to write to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akatz712
    New Member
    • Apr 2007
    • 9

    Use PHP ONLY to write to database

    Is there a way to use PHP to ONLY write to the database?

    I am calling a PHP using POST to send data to the server to be stored in the database. The logic to save it is in the PHP.

    What I want to happen is that the program calling the PHP (with a SAVE button) pops up a modal window indicating that the database operation was a success or not a success. What is happening now is that I am getting a blank page in the browser which is popping up these messages. And to get back to my page I need to use the browser back button. And for other reasons this is not good.
  • tolkienarda
    Contributor
    • Dec 2006
    • 316

    #2
    i would love to help but i strugle without the code

    eric

    Comment

    • Akatz712
      New Member
      • Apr 2007
      • 9

      #3
      This is not a question about any piece of code. It is a question to explain how to do something in PHP.

      I am new to PHP (this week).

      In my experience, each time a PHP file is "run" it sends an entire page back to the browser. So, then the user needs to press the back button to get back to where they were before. And in this case, I do not want that to happen.

      Comment

      • exoskeleton
        New Member
        • Sep 2006
        • 104

        #4
        Originally posted by Akatz712
        This is not a question about any piece of code. It is a question to explain how to do something in PHP.

        I am new to PHP (this week).

        In my experience, each time a PHP file is "run" it sends an entire page back to the browser. So, then the user needs to press the back button to get back to where they were before. And in this case, I do not want that to happen.
        Hi good sir...if you want not to load the entire page upon writing to the database...you may use AJAX for that ... there in no plain PHP code that can do that!

        Hope this tip helps...

        Comment

        • tolkienarda
          Contributor
          • Dec 2006
          • 316

          #5
          exoskelton is right about needing AJAX for a popup or .js either one and to do that in php you just need to echo out the code
          example
          [PHP]
          echo 'alert("whateve r", "attributes you want")';
          [/PHP]

          and that is a basic popup window if to write stuff to a databse you just use the normal database commands inside of a mysql_query function
          example
          [PHP]
          mysql_query("UP DATE thefull SET ftext = '$text' WHERE location = '$location'");
          [/PHP]
          this will update a table called thefull and set ftext to equla the varable $text in the table row where location is equal to the var $location

          and finally if you want to get a value from the database
          [PHP]
          $result = mysql_query("SE LECT user, pass FROM users WHERE pass = '$pass' AND user='$user'");
          [/PHP]

          if you need any more general information you can check out php.net or the countless tutorals across the web
          if you have programing experiance then php.net will probably have all the answers you need but if the programing thought process in knew to you then try some of the tutorials that start with defining vars and building basic logic structures

          the best of luck to you

          eric

          Comment

          • exoskeleton
            New Member
            • Sep 2006
            • 104

            #6
            thank you for your support sir eric... :)

            @ Akatz712 ... AJAX is the best way of adding record to the database without reloading the whole page sir. as well as retrieving and updating!

            more power

            Comment

            • Akatz712
              New Member
              • Apr 2007
              • 9

              #7
              The question should have been:
              How does the client keep control, and use the server as a servant and not just package something for the server and the server spits out a new page, leaving the original client out of the loop?

              The answer is AJAX. Since I am learning PHP, I still will write the server side code in PHP. But if my PHP does generate HTML, it will send it back to the client who called it, and the client decides where to place it in its world.

              I found some code online which I modified into a general purpose AJAX POST. My only thought is that this is the wrong forum. But here it is:

              Code:
                 // This AJAX library is taken from http://www.captain.at/howto-ajax-form-post-request.php
                 // I modified it to return the contents of the one text written by the PHP function in a passed handler.
                 var http_request = false;
                 var js_handler_pointer = null;
                 function makePOSTRequest(url, parameters, js_handler) {
                    http_request = false;
              	  js_handler_pointer = js_handler;
                    if (window.XMLHttpRequest) { // Mozilla, Safari,...
                       http_request = new XMLHttpRequest();
                       if (http_request.overrideMimeType) {
                       	// set type accordingly to anticipated content type
                          //http_request.overrideMimeType('text/xml');
                          http_request.overrideMimeType('text/html');
                       }
                    } else if (window.ActiveXObject) { // IE
                       try {
                          http_request = new ActiveXObject("Msxml2.XMLHTTP");
                       } catch (e) {
                          try {
                             http_request = new ActiveXObject("Microsoft.XMLHTTP");
                          } catch (e) {}
                       }
                    }
                    if (!http_request) {
                       alert('Cannot create XMLHTTP instance');
                       return false;
                    }
                    
                    http_request.onreadystatechange = alertContents;
                    http_request.open('POST', url, true);
                    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    http_request.setRequestHeader("Content-length", parameters.length);
                    http_request.setRequestHeader("Connection", "close");
                    http_request.send(parameters);
                 }
              
                 function alertContents() {
                    if (http_request.readyState == 4) {
                       if (http_request.status == 200) {
                          //alert(http_request.responseText);
                          js_handler_pointer(http_request.responseText);
                       } else {
                          alert('There was a problem with the request.');
                       }
                    }
                 }
              // How to call this is done by simply giving string to the PHP in the form: 
              // makePOSTRequest('post.php',"Name1=encodeURI(value1)&Name2=encodeURI(value2)...", js_handler);
              Since this is a PHP forum, I feel obligated to include my PHP code:

              [PHP]<?php
              function insertsql($item , $key)
              {
              if (substr($key, 0, 6)=='PMAGIC')
              {
              global $records, $name;
              $query = 'INSERT INTO DesignSession VALUES("'.$name .'",'.substr($i tem,1,strlen($i tem)-2).')';
              $result = mysql_query($qu ery) or die('Insert main failed: ' . mysql_error());
              $records++;
              }
              }
              $name=$_POST['Savename'];
              $records = 0;
              if ($name=="")
              $responseText=" Save name not entered.";
              else
              {
              $link = mysql_connect(' localhost','roo t','') or die('Could not connect: ' . mysql_error());
              mysql_select_db ('akatz712_mgs' ) or die('Could not select database');

              $query = 'SELECT * FROM DesignSession_B ackground WHERE Name="'.$name.' "';
              $result = mysql_query($qu ery) or die('Query failed: ' . mysql_error());
              if (mysql_num_rows ($result)!=0)
              {
              $responseText=" Save name ".$name." already used. Pick another.";
              }
              else
              {
              $backcolor = substr($_POST['backgroundcolo r'],1,strlen($_POS T['backgroundcolo r'])-2);
              $query = 'INSERT INTO DesignSession_B ackground VALUES("'.$name .'",'.$backcolo r.',NULL)';
              $result = mysql_query($qu ery) or die('Insert background failed: ' . mysql_error());
              array_walk($_PO ST, 'insertsql');
              $responseText=" Design Session ".$name." successfully saved with ".$records. " design(s).";
              }
              mysql_close($li nk);
              }
              echo $responseText;
              ?>
              [/PHP]

              Comment

              Working...