+ Reply to Thread
Results 1 to 7 of 7

Thread: Run script in current document.

  1. #1
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Run script in current document.

    At the moment I use this;
    <form name="form1" method="post" action="scripts/update.php">

    I want to add the php script under my form code so that the echo function will write the text in the current document, because my current action loads a new page (update.php).

    I have added a function to the script and included into the document under the form, now what do I type into the action="" to call the function?

    PHP Code:
         <form name="form1" method="post" action="scripts/update.php">
            <p class="g2">Name:</p>
            <input type="text" class="contact-feild" name="name" size="20" />
            <br />
            <p class="g2">Email:</p>
            <input type="text" class="contact-feild" name="email" size="20" />
            <br />
            <p class="g2">Message:</p>
            <textarea name="message" class="contact-feild" style="width:300px; height:150px; overflow:scroll; overflow-x:hidden;">
      </textarea>
            <br />
            <input type="submit" value="Submit" class="button" name="submit"/>
          </form>
          <?php include 'scripts/update.php'?>
    update.php
    PHP Code:
    <?php
    function sendData()
    {
    // Connect to database
    $con mysql_connect("localhost","martynba_martynb","password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
      
    // Select table
    mysql_select_db("martynba_comments"$con);

    // Make varibles
    date_default_timezone_set('GMT');
    $name mysql_real_escape_string($_POST['name']);
    $email mysql_real_escape_string($_POST['email']);
    $message mysql_real_escape_string($_POST['message']);
    $date date("F j, Y, g:i a");

    // Check data has been inputted
    if (empty($name)) {
        echo 
    'Please enter your name! <br />';
        }

    // Insert data
    $query="INSERT INTO commentTable ( NAME, EMAIL, MESSAGE, DATE)
    VALUES ('"
    .$name."','".$email."','".$message."','".$date."')";
    $result mysql_query ($query);
        
    header('Location: http://martynleeball.x10hosting.com/thanks.php');
        }
    ?>
    Last edited by martynball; 11-28-2009 at 03:26 PM.

  2. #2
    drf1229 is offline x10Hosting Member drf1229 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    71

    Re: Run script in current document.

    Just delete the function from the php file and it will run fine. You also do not need "include" if the user is submitting a form. If you are looking into recieving data from the php file without loading the php page or refreshing the page, I think you need to look into ajax. You can find a great tutorial at http://www.w3schools.com .

  3. #3
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Re: Run script in current document.

    Does that mean re-coding the script which saves the data to the database in AJAX language?

    The "echo" is running before I click submit... how do I make it only run when I have submited?
    PHP Code:
    <?php
    // Connect to database
    $con mysql_connect("localhost","martynba_martynb","password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
      
    // Select table
    mysql_select_db("martynba_comments"$con);

    // Make varibles
    date_default_timezone_set('GMT');
    $name mysql_real_escape_string($_POST['name']);
    $email mysql_real_escape_string($_POST['email']);
    $message mysql_real_escape_string($_POST['message']);
    $date date("F j, Y, g:i a");

    // Insert data
    $query="INSERT INTO commentTable ( NAME, EMAIL, MESSAGE, DATE)
    VALUES ('"
    .$name."','".$email."','".$message."','".$date."')";
    $result mysql_query ($query);
     echo 
    "Done!";
    ?>
    Last edited by martynball; 11-28-2009 at 04:51 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Run script in current document.

    What exactly do you want to do?

    Do you want to present the user with a comment form, have the user submit that form, and if

    1) he does not fill it out properly, send the form again with an error message
    2) he does fill it out properly, you save the info and redirect him to a thank you page

    Is that what you want to do?

  5. #5
    martynball is offline x10Hosting Member martynball is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Stoke-on-Trent, UK
    Posts
    60

    Re: Run script in current document.

    I have it solved now, never mind.

  6. #6
    drf1229 is offline x10Hosting Member drf1229 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    71

    Re: Run script in current document.

    Quote Originally Posted by martynball View Post
    Does that mean re-coding the script which saves the data to the database in AJAX language?

    The "echo" is running before I click submit... how do I make it only run when I have submited?
    PHP Code:
    <?php
    // Connect to database
    $con mysql_connect("localhost","martynba_martynb","password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
      
    // Select table
    mysql_select_db("martynba_comments"$con);

    // Make varibles
    date_default_timezone_set('GMT');
    $name mysql_real_escape_string($_POST['name']);
    $email mysql_real_escape_string($_POST['email']);
    $message mysql_real_escape_string($_POST['message']);
    $date date("F j, Y, g:i a");

    // Insert data
    $query="INSERT INTO commentTable ( NAME, EMAIL, MESSAGE, DATE)
    VALUES ('"
    .$name."','".$email."','".$message."','".$date."')";
    $result mysql_query ($query);
     echo 
    "Done!";
    ?>
    First of all, ajax is NOT a separate language. It is a combination of javascript, php, and xml. It is really simple and you will NOT need to retype your entire page. Just save the php file that writes to the database into a separate file. Actually look at the tutorials and you will see :D.

  7. #7
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Run script in current document.

    Quote Originally Posted by drf1229 View Post
    First of all, ajax is NOT a separate language. It is a combination of javascript, php, and xml. It is really simple and you will NOT need to retype your entire page. Just save the php file that writes to the database into a separate file. Actually look at the tutorials and you will see :D.
    The Ajax means Asynchronous Java Script + XML You can use it with any other programming languages like php, ASP.NET etc...

    Data is usually retrieved using the XMLHttpRequest object. Despite the name, the use of JavaScript and XML is not actually required, nor do the requests need to be asynchronous.

    source --> wikipedia
    Last edited by Gouri; 11-29-2009 at 10:00 AM.
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

+ Reply to Thread

Similar Threads

  1. [OFF] selling some hot script 500 credits each
    By neteater in forum The Marketplace
    Replies: 8
    Last Post: 03-24-2009, 09:08 PM
  2. CRON job : script timeout ?
    By webtomata in forum Free Hosting
    Replies: 4
    Last Post: 09-12-2008, 04:23 PM
  3. Access to 300+ PHP scripts (2500 credits)
    By jonathanyaniv in forum The Marketplace
    Replies: 11
    Last Post: 06-03-2008, 10:58 PM
  4. Replies: 8
    Last Post: 12-03-2007, 04:12 PM
  5. Server UP time Script
    By dharmil in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 04-03-2006, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers