Run script in current document.

martynball

New Member
Messages
60
Reaction score
0
Points
0
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:
     <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:
<?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:

drf1229

New Member
Messages
71
Reaction score
1
Points
0
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 .
 

martynball

New Member
Messages
60
Reaction score
0
Points
0
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:
<?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:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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?
 

drf1229

New Member
Messages
71
Reaction score
1
Points
0
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:
<?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.
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
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:
Top