Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two php documents first one is reg.php, it has connection string and insert query to the database and in the other php document signup.php, it has one textbox as first name and signup CSS button. Once press the sign up button, error occurred as Undefined index: firstname, kindly advise me how to get signup.php firstname textbox data to the reg.php?

Your soonest response is highly appreciated.

XML
reg.php
//connection string is working properly
$connection = mysql_connect($host,$dbuser,$dbpass);
$db = mysql_select_db($dbname, $connection);
$firstName_txt = $_GET['firstname'];
$sql = "INSERT INTO Test (Rock) VALUES ($firstName_txt)";
$result = mysql_query($sql);
echo "Thank you";


signup.php
<form name="reg" method="POST" action="reg.php">
<input type="text" name="firstname" id="firstname2" style="font-family:'Myriad Pro';font-size:16px"/>
<div id="cssButton"> <a href="reg.php" class="button custom">Sign Up</a></div>
</form>


[Edited Question]
Now the problem is typed value is not going to the database, always going 1 as a value.

reg.php
//connection string is working properly
$connection = mysql_connect($host,$dbuser,$dbpass);
$db = mysql_select_db($dbname, $connection);
$firstName_txt = isset($_POST['firstname']);
$sql = "INSERT INTO Test (Rock) VALUES ($firstName_txt)";
$result = mysql_query($sql);
echo "Thank you";

signup.php
<form name="reg" method="POST" action="reg.php">
<input type="submit" name="POST" value="Register"/>
</form>
Posted
Updated 8-Jul-13 8:36am
v2

1 solution

Use $_POST["firtname"]. Look at your HTTP request method: it's POST.

Besides, before working with any PHP object, at least with the one coming from outside of your code, it's good to check up if it is initialized or not. This is how:
http://php.net/manual/en/function.isset.php[^].

For comparison, see also: http://php.net/manual/en/function.defined.php[^].

In your case, you can really get unset object be the following reason: someone can load your page not as the action of your form, but simply by loading it by the URI in the browser (by whatever reason). You should be able to handle such situations in some reasonable way. In practice, many make the page with the form, and its form action the same. In this case, you would need to distinguish between loading this page with POST and without it.

[EDIT]

Your second problem is <a href="reg.php">. You simply request and load "reg.php" without the post, hence the problem. You need to use a Submit button instead:
XML
<input type="submit" value="Register"/>


—SA
 
Share this answer
 
v6
Comments
abdulsafran 8-Jul-13 11:13am    
Hi Sergey, thanks for your response. I have change my code to $firstName_txt = $_POST['firstname'];, but still the same problem, could you please recheck and let me know what are the changes to be done? Your soonest response is highly appreciated.
Sergey Alexandrovich Kryukov 8-Jul-13 11:17am    
Well, did you use isset? How did you load "reg.php"? through the form or not?
—SA
abdulsafran 8-Jul-13 11:21am    
I have changed as $firstName_txt = isset($_POST['firstname']);, at this time no error messages and data is not insert to the database. reg.php load from form. Kindly check advise.
Sergey Alexandrovich Kryukov 8-Jul-13 11:24am    
I fixed your other bug, please see. You did not actually submit a form.
And use isset, you did not do it.
—SA
abdulsafran 8-Jul-13 11:30am    
This time according to your advice, I have changed $firstName_txt = isset($_POST['firstname']); and <input type="submit" value="Register"/> in the two forms. Now, data is transfer to the database, but every time goes as 1 not what I type in the firstname textbox, kindly check and advise?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900