Jump to content

Help With Complicated Php Form


LivingReceiver

Recommended Posts

Hey! My name is Molly and I am currently attempting to create a form for use in a science project I am conducting.

 

This form has inputs for the usual name, age, gender, etc. The only difference is, after the subjects are done filling out the name, age, and gender inputs, they need to be taken to the next page that will give them four options. Each option will have a three letter (or two letter, if their name is three letters or less) prefix (I can create all of these, they all have to be completely different) , and the same ending. (For example, 'poln'.) The fourth option will take the first three letters of the submitted name (or two letters, if their name is three letters long or less) and tack it onto the same ending. ('poln'). Then the subject needs to be able to choose this option, and submit all the information to my sql database.

 

Here's an example situation;

 

Your name is Sarah. You go to the website and type in your name, age, and click on your gender. You click the next button, and you see a page with four choices;

Molpoln

Addpoln

Sarpoln

Tarpoln

 

You select your choice and press submit, which sends the information to a data base.

 

Now have pretty much no experience with php, this is incredibly difficult for me. I have understood how to great the normal inputs i.e. text boxes for name/age and radio buttons for gender, but the rest is almost impossible.

 

In short,

1. How do I "fix" the name input so that the code will take the first three letters and tack it onto the ending?

2. How do I "upload" the other choices so that whenever the name is given, the other three choices must start with a different letter?

3, How do I make these options clickable?

4. How do I submit it all to my sql database?

 

This is what I have so far for my index page

 

<html>

<body>

<style>

.robotext {font-weight: bold; font-size: 9pt; color: #999999; font-family: Arial, Helvetica, sans-serif; text-decoration: none}

.robolink:link {font-weight: bold; font-size: 9pt; color: #999999; font-family: Arial, Helvetica, sans-serif; text-decoration: none}

.robolink:hover {font-weight: bold; font-size: 9pt; color: #979653; font-family: Arial, Helvetica, sans-serif; text-decoration: underline}

.robolink:visited {font-weight: bold; font-size: 9pt; color: #979653; font-family: Arial, Helvetica, sans-serif; text-decoration: none}

</style>

<script language="Javascript">

function validate(){

var allok = true;

if(sproject.First_Name__1.value == ""){

alert('Invalid input for First Name');

return false;

}

if(isNaN(sproject.Age__2.value)){

alert('Invalid input for Age, this must be a number')

return false;

}

document.sproject.Submit.disabled="disabled";

return true;

}

</script>

<form name="sproject" method="Post" action="http://agreatperhaps...nf/entries.php" onsubmit="return validate();">

<form method="post" action="update.php"

<table width="100%" border="0" cellpadding="5" cellspacing="0">

<tr><td>First Name</td><td><input type="edit" name="name1" value="" size="15"></td></tr>

<tr><td>Age</td><td><input type="edit" name="age2" value="" size="2"></td></tr>

<tr><td valign=top>Gender</td><td valign=top>

<input type="radio" name="gen3" value="Female" checked>Female<br>

<input type="radio" name="gen3" value="Male">Male<br>

</td></tr>

<tr><td colspan=2><input type="submit" name="Submit" value="submit"></td></tr>

 

 

 

</body>

</html>

 

 

And here is my update.php page

 

<html>

<body>

<?php

$name1 = $_POST[ ' name1 ' ];

$age2 = $_POST [ 'age2 '];

$gen3 = $_POST [ 'gen3 '];

 

mysql_connect ("provider", "databasename", "databasepassword") or die ('Error: ' . mysql_error ())

mysql_select_db ("databsename");

$query= "INTERT INTO TestTable (name1, age2, gen3) VALUES ('NULL', '".$name1."', '".$age2."', '".gen3."')

mysql_query ($query) or die ('Error updating database');

 

echo "Database Updated With: " .$name1." ".$age2."

 

?>

</body>

</html>

 

 

Thank you so much!

Edited by LivingReceiver
Link to comment
Share on other sites

For your first question you'll want to use substr (), the PHP manual will give you the details on it.

 

For the second question, you do not as much "upload" the other options as you generate and echo them to the client. Remember that PHP is running on the server, and is used to generate and send the HTML code to the browser. So you have to separate those two processes in your mind, in order to keep from becoming overwhelmed and confused. ;)

 

In the third question it sounds like you want to either use a SELECT drop down, or radio buttons for this one. Both of which are standard HTML, and a part of regular form handling.

However, you will need to use sessions to hold the answers and the previously submitted data between the page loads. Otherwise the server will just toss all that away, and have no idea on what the "correct" answer is.

 

Lastly for the DB insertion: I'd wait until after the "correct" answer has been given, and then insert it into the DB as you've already done in the code above. Fetching the data from the $_SESSION array.

Though, you really need to read up on Input Validation and Output Escaping to secure your code. As it is now your script is wide open for any attackers to abuse as they see fit, and trust me you will be attacked.

 

Last recommendation I have is to look into PDO and/or MySQLI. The old MySQL library (without the i) is outdated and no longer being developed, meaning it'll be removed from PHP soon(ish). So best to start with the method that you can use in the future as well, and save yourself from the headache of having to fix your scripts when they remove the old methods. ;)

 

Oh, almost forgot. Please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read. It's also a lot easier than to manually highlight your code. ;)

Edited by Christian F.
Link to comment
Share on other sites

Thank you so much for the help!

 

I got the whole three letter name + poln thing down, but I'm having trouble adding it into a radio button.

Any suggestions?

 

 <?php
$email = "myemail@email.com";


print "<p>The following information was submitted from the form:<p><table width=\"450\" border=\"0\"><tr><td style=\"BORDER: #efecd6 1px solid;\"><table border=\"0\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\">
<tr><td width=\"35%\">First Name</td><td>".$_REQUEST[name1]."</td></tr>
<tr><td width=\"35%\">Age</td><td>".$_REQUEST[age2]."</td></tr>
<tr><td width=\"35%\">Gender</td><td>".$_REQUEST[gen3]."</td></tr>
</table></td></tr></table>
";
$message = "The following information was submitted from the form on your website:\n";
$message .= "First Name: ".$_REQUEST[name1]."\n\n";
$message .= "Age: ".$_REQUEST[age2]."\n\n";
$message .= "Gender: ".$_REQUEST[gen3]."\n\n";
mail( $email, "myemail@email.com", $message, "From: $email
X-Priority: 1 (Highest)" );
?>

<?php
   $var = $_POST['name1'];
$please = echo substr_replace($var, 'poln', 3) . "<br />\n";


?>


 <p> Imagine you are at a grocery store and you see the four sodas listed below. Which one would you choose? </p>


<form method="POST" action="testsend.php">


<p><select name="select1" size="1">
<input type= "radio" value="annies"></input>


<input type= "radio" value="pears"></input>


<input type= "radio" value="$please"></input>


</select></p>
<p><input type="submit" name="submit"> </p> </p>


</form>

 

I'm also having trouble figuring out how to make a list of 3 letter phrases to put in front of 'poln' and insert them, while making sure they don't start with the same letter as $please. (Am I even able to make the substr_replace () a variable?)

 

Do I make a php file and list them all under the same variable?

 

So sorry for all the questions, I'm completely new to PHP ><.

 

Thanks to everyone for all your help!

 

P.S. Christian, do you think protecting the website will be necessary since I am just really using the url on a school computer? I'm not really promoting it too much online. (It's for a science project I'm conducting.)I'm not sure. Thank you so much for your help! :)

Edited by LivingReceiver
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.