Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 01-07-2008, 04:30 PM   #1 (permalink)
Perpetual Fresh Stock
 
siddes's Avatar
 
Join Date: Feb 2007
Location: Jaipur
Posts: 50
Default How to stop form submission redirecting to PHP page?


Hi,

I'm working on a comments system and whenever I click on submit, the page redirects to the php file.

I don't want this happening, I just want to submit my data to the file using ajax, have the php file write that data to another file without any kind of response except success or failure.

The response after the submit button has been clicked will be given by my script.


Any ideas?
__________________
http://sid-deswal.110mb.com

No clever lines here, go think them up yourselves.
siddes is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 01-07-2008, 09:21 PM   #2 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: How to stop form submission redirecting to PHP page?

Use onsubmit event. When the form is submitted, trigger AJAX function and return false so that the form is not submitted. The advantage is that if javascript is disabled, the comment will be submitted as a normal form submission. So prepare the server side comment-adding script accordingly.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 02-07-2008, 02:23 AM   #3 (permalink)
MMO Addict
 
amitava82's Avatar
 
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
Default Re: How to stop form submission redirecting to PHP page?

Use jQuery library+form plugin for this. Here is an example:

1. register.html file:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

 <script type="text/javascript" src="jquery-1.2.6.min.js"></script>    
<script type="text/javascript" src="jquery.form.js"></script>          
 <script type="text/javascript">                                         
  
$(document).ready(function() {

$('#signup').submit(function(){

	var options = {
		target: '#response',
		success: function(){
		$('#response').fadeIn('medium');
		}
	};

    $(this).ajaxSubmit(options);
    return false; 
});

});
  
 </script>
 <style type="text/css">
<!--
#response {
	background-color: #FFFFCC;
	display: none;
	color: #FF0000;
}
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
}
-->
 </style>
</head>

<body>
<h2>Register:</h2>
<form id="signup" name="signup" method="post" action="register.php">
  <table width="200" border="0">
    <tr>
      <td><label>Name: </label></td>
      <td><input type="text" name="name" id="name" /></td>
    </tr>
    <tr>
      <td>Email: </td>
      <td><input type="text" name="email" id="email" /></td>
    </tr>
    <tr>
      <td><label></label></td>
      <td><input type="submit" name="submit" id="submit" value="Submit" /></td>
    </tr>
  </table>
</form>
<div id="response"></div>
</body>
</html>
2. register.php file
PHP Code:
<?php

$name 
trim($_POST['name']);
$email =  trim($_POST['email']);

if (
$name =="" && $email == ""){

    echo 
"Please check your input and try again.";
}

else {

    echo 
"Thank you. Here are the informations you have submitted: <br />";
    echo 
"Name: ".$name"<br />";
    echo 
"Email: ".$email"<br />";

}
 
?>
__________________
Steam Profile || Personal Page
Warp drive active. Approaching stargate.
amitava82 is offline  
Old 03-07-2008, 01:25 PM   #4 (permalink)
Perpetual Fresh Stock
 
siddes's Avatar
 
Join Date: Feb 2007
Location: Jaipur
Posts: 50
Default Re: How to stop form submission redirecting to PHP page?

Well, I'm having a little problem.

The code from where I submit my comments is

Code:
<html>
<head>
<script src="jquery.js">
</script>


<script>
$(document).ready(function() {
    $('#submitForm').bind('submit', function() {
        var username = $('#uname').val();
        var usercomment = $('#ucomment').val();
        alert (username + " says " + usercomment);
        $.post("q.php", {'uname: username, ucomment: usercomment'}, alert("done"));
        return false;
    })
})

</script>

<form name="asd" id="qwe" action="q.php" method="post">
Name <input type="text" id="uname"><br>
Message <input type="text" id="ucomment"><br>
<input type="Submit" value="Submit" id="submitForm">
</form>
And my PHP file is
Code:
<?php
    $filename = "frontend.htm";
    if (isset($_POST['uname'])) {
        $username = $_POST["uname"];
    } 
       
    if (isset($_POST['ucomment'])) {
        $usercomment = $_POST["ucomment"];
    }
        
    if(!($myFile = fopen($filename, "a")))
    {
        print("Error: ");
        print("'$filename' could not be created\n");
        exit;
    }

    //Trim and Create special characters into respective HTML entities
    $named = htmlentities(trim($username));
    $commented = htmlentities(trim($usercomment));
    echo "This works";
    
    //Write to file
    fwrite($myFile, "<p><b>$named says</b> says<br>");
    fwrite($myFile, "$commented<br></p>\n");

    //close the file
    fclose($myFile);

?>

The error I get is

Notice: Undefined variable: username in c:\program files\easyphp1-8\www\new\q.php on line 19

Notice: Undefined variable: usercomment in c:\program files\easyphp1-8\www\new\q.php on line 20

By the way, I'm using EasyPHP 1.8 which consists of apache 1.3.33 - php 4.3.10 - mysql 4.1.9 - phpmyadmin 2.6.1
__________________
http://sid-deswal.110mb.com

No clever lines here, go think them up yourselves.
siddes is offline  
Old 03-07-2008, 08:31 PM   #5 (permalink)
MMO Addict
 
amitava82's Avatar
 
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
Default Re: How to stop form submission redirecting to PHP page?

Your form should be like this:
Code:
<form name="asd" id="qwe" action="q.php" method="post">
Name <input type="text" id="uname" name="uname"><br>
Message <input type="text" id="ucomment" name="ucomment"><br>
<input type="Submit" value="Submit" id="submitForm">
</form>
You call post value using field name.
__________________
Steam Profile || Personal Page
Warp drive active. Approaching stargate.
amitava82 is offline  
Old 03-07-2008, 11:42 PM   #6 (permalink)
Perpetual Fresh Stock
 
siddes's Avatar
 
Join Date: Feb 2007
Location: Jaipur
Posts: 50
Default Re: How to stop form submission redirecting to PHP page?

Found the problem.

Its in the jQuery code
Code:
$.post("q.php", {'uname: username, ucomment: usercomment'}, alert("done"));
The values passed to the php file should not contain inverted commas
Code:
$.post("q.php", {uname: username, ucomment: usercomment}, alert("done"));
__________________
http://sid-deswal.110mb.com

No clever lines here, go think them up yourselves.
siddes is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting "My Documents" folder karnivore Tutorials 3 01-04-2007 07:04 PM
MS WORD Page Numbering (ommiting 1,2,3 page) Any body Butterfly Software Q&A 1 24-03-2005 12:48 AM
Website Submission in Search Engines - editted help_me QnA (read only) 26 21-03-2005 07:54 PM
Search Engine Optimization & Submission senthilrek QnA (read only) 1 01-12-2004 10:24 AM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:05 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2