RLJ Posted October 4, 2010 Share Posted October 4, 2010 Hi, I currently have an HTML form that posts data (method:POST) to a PHP script. The PHP script then retrieves the data and displays it on screen so the user can do a final check. I then want to have a button, so that when pressed, the data gets sent to a MySQL database. What I have so far is along these lines: <?phpif(isset($_POST['var1'])) {$var1 = $_POST['var1'];} else {$var1 = "";}if(isset($_POST['var2'])) {$var2 = $_POST['var2'];} else {$var2 = "";}echo "<b>You have entered the following:</b><p></p>", "$var1 <br>","$var2<p></p>", "If this is correct, please press continue.";print "<html><body><button name=Button1 onclick="">Continue</button></body></html>"; and onclick I want the following to run: $link = mysql_connect ('localhost', 'root');if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db ('DB1', $link);mysql_query ("INSERT INTO table1 (field1, field2) VALUES ('$var1','$var2')");mysql_close($link); How can I do this?! Is it neccesary to use another "submit" button that submits $var1 & $var2 to a different PHP script, which then runs the mysql_connect part? Or can I enclose the mysql_connect part in an IF statement that runs only if Button1 has been pressed (and I enclose the rest of the PHP script in an IF statement that runs only if Button1 has NOT been pressed) and get Button1 to reload the same PHP script? Either way, how do I define an onclick event inside echo? This prob a noobish question, but I seem to get errors if I try to put (single) quotes "" ' ' inside the quotes of the echo: echo " onclick"" "; Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/ Share on other sites More sharing options...
liamoco Posted October 4, 2010 Share Posted October 4, 2010 On the final check page, you could create some hidden inputs and the values as $var1 and $var2 and put them into another form and post it again. echo "<b>You have entered the following:</b><p></p>", "$var1 <br>","$var2<p></p>", "If this is correct, please press continue.";print "<html><body><form action='file.php' method="POST"><input type='hidden' name='var1' value='{$var1}'><input type='hidden' name='var2' value='{$var2}'><submit name="submit_data" value="Send">form></body></html>"; Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/#findComment-1118843 Share on other sites More sharing options...
Adam Posted October 4, 2010 Share Posted October 4, 2010 'onclick' suggests you mean to submit the data without the page actually navigating away from the page. Is that what you're trying to describe? Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/#findComment-1118845 Share on other sites More sharing options...
RLJ Posted October 4, 2010 Author Share Posted October 4, 2010 Thanks liamoco, I'll give your idea a try. Ideally though, I would prefer not navigating away from the page and running a separate PHP script, as MrAdam suggests. Is there a way to run the mysql_connect bit within the same PHP script, but only if a button is pressed? Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/#findComment-1118869 Share on other sites More sharing options...
chintansshah Posted October 4, 2010 Share Posted October 4, 2010 Please use password for third parameter in mysql_connect correct one is $link = mysql_connect ('localhost', 'root',''); Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/#findComment-1118881 Share on other sites More sharing options...
Adam Posted October 4, 2010 Share Posted October 4, 2010 Please use password for third parameter in mysql_connect correct one is $link = mysql_connect ('localhost', 'root',''); Actually if a password hasn't been set you don't *need* to pass the third parameter, unless you've set a default in the php.ini configuration file. Thanks liamoco, I'll give your idea a try. Ideally though, I would prefer not navigating away from the page and running a separate PHP script, as MrAdam suggests. Is there a way to run the mysql_connect bit within the same PHP script, but only if a button is pressed? First get the form working through the normal submission method, to ensure once you add in the extra "AJAX" functionality you don't make it unusable for those with JavaScript disabled. Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/#findComment-1118910 Share on other sites More sharing options...
RLJ Posted October 4, 2010 Author Share Posted October 4, 2010 yeah the form works fine through the normal submission method: all required variables are passed from the form to the PHP script and from the PHP script to MySQL (if I include the mysql_connect part) All I want to do now is add an extra step between PHP and MySQL where the variables are only passed on if a button is pressed. Quote Link to comment https://forums.phpfreaks.com/topic/215114-mysql_connect-button/#findComment-1118953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.