Jump to content

mysql_connect button


RLJ

Recommended Posts

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!

Link to comment
Share on other sites

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>";

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.