Jump to content

submit data from a form with the php script on the same page


dazz_club

Recommended Posts

Hi guys,

 

On my site i have a input field for users to join my mail bag. So all it has is on field and a submit button.

 

I trying to have the form and the script that submits the form on same page. I would like it this way because I would like to display a message, saying something like thank you, "you have been added"

 

I had trouble submiting the data when it was on the form, so i created a seperate script.

 

MY question is how much of the script would i need change to put it with the form page and for it to work??

 

Hope you understand

 

Below is the script to submit the form data (well insert the data in my database)

 

mailbag.php

------------

<?php

 

$email=$_POST['email'];

 

if (!$email)

{

echo 'You must enter data in all of the field'.'Plumb';

exit;

}

if (!get_magic_quotes_gpc())

{

$email = addslashes($email);

}

 

@$db = new mysqli('localhost', 'root', 'xxx', 'xx');

 

if (mysqli_connect_errno())

{

echo 'Error.';

exit;

}

 

$query = "INSERT INTO mail_bag  (email_add) VALUES ('".$email."')";

 

$result = $db->query($query);

if ($result)

echo $db->affected_rows.'email inserted.';

$db->close();

 

?>

 

------------

 

This is the page with the form, join-mailbag.php

 

----------------

<form method="post" action="<? echo $_SERVER['PHP_SELF'] ?>">

<ul id="mailBag">

<li><input type="text" name="email" class="mailBagTextBox" ></li>

<li><input type="image"  value="submit" src="images/site-images/mailbag-bttn.png" ></li>

</ul>

</form>

---------------

 

currently the sate of play is that i have been attempting to combine the two on one page hence the  action="<? echo $_SERVER['PHP_SELF'] ?>  instead of  action="mailbag.php"

 

Kind regards

Dazzclub

Link to comment
Share on other sites

Hi , should have thought of that really.

 

Ok, after reading your guys postshave a merged them together and the code looks something like;

 

<?php

$email=$_POST['email'];

if (!$email) 
{
echo "<form method=\"post\" action=\"<? echo $_SERVER['PHP_SELF'] ?>\" >" ;
echo "<ul id=\"mailBag\">";
echo "<li><input type=\"text\" name=\"email\" class=\"mailBagTextBox\" ></li>";
echo "<li><input type=\"image\"  value=\"submit\" src=\"images/site-images/mailbag-bttn.png\" ></li>";	
echo "</ul>";
echo "</form>";
exit;
}
if (!get_magic_quotes_gpc())
{
$email = addslashes($email);
}

@$db = new mysqli('localhost', 'root', 'DARREN', 'tlc-line');

if (mysqli_connect_errno())
{
echo 'Error.';
exit;
}

$query = "INSERT INTO mail_bag  (email_add) VALUES ('".$email."')";

$result = $db->query($query);
if ($result)
	echo $db->affected_rows.'email inserted.';
$db->close();

?>

 

When i go to try it out it displays;

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\wamp\www\tlc-line.com\includes\join-mailbag.php on line 7

 

 

I think i may know what the cause is to this, so i'll post back to see if i have corrected it.

 

Thanks again for your replies and help.

 

Kind regards

Dazzclub

 

 

Link to comment
Share on other sites

hi all,

 

Just thought i'll post back and to say the script is now working

 

Here is the script

----------------

<?php

 

$email=$_POST['email'];

 

if (!$email)

{

echo "<form method=\"post\" action=\"".  $_SERVER['PHP_SELF'] . "\" >";

echo "<ul id=\"mailBag\">";

echo "<li><input type=\"text\" name=\"email\" class=\"mailBagTextBox\" ></li>";

echo "<li><input type=\"image\"  value=\"submit\" src=\"images/site-images/mailbag-bttn.png\" ></li>";

echo "</ul>";

echo "</form>";

return;

}

if (!get_magic_quotes_gpc())

{

$email = addslashes($email);

}

 

@$db = new mysqli('localhost', 'root', 'DARREN', 'tlc-line');

 

if (mysqli_connect_errno())

{

echo 'Error.';

exit;

}

 

$query = "INSERT INTO mail_bag  (email_add) VALUES ('".$email."')";

 

$result = $db->query($query);

if ($result)

echo "<div style=\"color:red;font-weight:bold;\">You have now been added. Thank you.</div>";

echo "<form method=\"post\" action=\"".  $_SERVER['PHP_SELF'] . "\" >";

echo "<ul id=\"mailBag\">";

echo "<li><input type=\"text\" name=\"email\" class=\"mailBagTextBox\" ></li>";

echo "<li><input type=\"image\"  value=\"submit\" src=\"images/site-images/mailbag-bttn.png\" ></li>";

echo "</ul>";

echo "</form>";

?>

----------------

 

If anyone spots anything, like uneccessary code that would be great.

 

Thanks for all your help.

 

kind regards

Dazzclub

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.