Please Don't Shoot!!!!

Hello all. I’m very obviously a noob when it comes to php and webdesign, but luckily for your internet browsing tranquility, I have no desire to become one! That said, I DO have the desire to play around with some very basic forms and toss the info to php. So let me tell you what I’m looking to do and maybe someone can help me.

As an example, I want to have an html for that takes my NAME, AGE, and LOCATION. Next I have to get it to a php file. But I don’t want it to be the same php file every single time I press submit. So, my thought process is (and please bare with me and try to make sense of what I’m trying to achieve as I’m not a programmer), but I’m thinking that I’ll have 3 php files. One of them will greet me with:

Hey “NAME”, you are “AGE” and you live in “LOCATION”.

Another greeting will be:

Hey “NAME”, good to see you again! Are you still “AGE”? And do you still live at “LOCATION”?

And the last something like:

What are you up to, “NAME”? How’s life in “LOCATION”? “AGE” huh, not getting any younger are you?!

So, with that said, my initial thought is, I have to create a php file, lets say, random.php, that randomizes which php file those variables will post to. Like for example, the response php files can be named response1.php, response2.php, and response3.php. Where the random files tells it to reply with response[random].php so each time I press the submit button, it present a different response. Does that make sense? I’m sorry I’m such a noob but I have some ideas I want to play with that require the random response and I’m afraid that, as a beginner, if I do something wrong, my website will get hacked all over the place. :frowning: I also want to add reCAPTCHA to the bottom of the form so bots don’t blast my servers.

This is just a very simple example so I can get the idea of how to achieve those results. Can anybody help me? Thanks for your time!!

Sound’s like you know what you want…

Just start slowly…

You can accomplish everything on a single.php page, if you want.

Break the task down into mini goals.

  1. Create a php page, that allows a user to type in NAME, AGE, LOCATION, once you get that working that’s a big step.
  2. Then add a submit button to the page.
  3. Then retrieve the values the user put in and display them (Now you know how to access them) $_GET[‘name’]
  4. Then generate a random # between 1 and 3
  5. Then based on the number use a if/then or switch statement to output your desire phrase.

If you get stuck at any point, ask for specific help, answering a simple question is fast.

Hey Topcoder. Thank you for replying!

The problem is, I am already stuck! :lol: I don’t have any idea where to begin. Well, that’s not entirely true. I know how to write an html form with entry fields and how to give them variables, but I don’t know anything about php other than they start with <?php and end with ?>, right? :stuck_out_tongue:

I do know what I want, I just don’t know how to achieve it. I stayed up until 2am last night searching the web trying to find something similar. I think I’m jsut gonna end up hiring a websdesigner. lol Too much for me.

Weird request :stuck_out_tongue: but anyways here is your code.

index.php
[php]

Random Response
<p>
	<label for="name"> Name: </label>
	<input type="text" name="name" id="name">
</p>

<p>
	<label for="age"> Age: </label>
	<input type="text" name="age" id="age">
</p>

<p>
	<label for="location"> Location: </label>
	<input type="text" name="location" id="location">
</p>

<p>
	<input type="submit" name="submit" value="Submit">
</p>
[/php]

response1.php
[php]

Response1 <?php if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['location']) ) {
		echo "<h1> Hey {$_POST['name']}, you are {$_POST['age']} and you live in {$_POST['location']}.</h1>";
	}

	else {

		echo "<h1> Please fill in all the fields </h1>";

	}

?>

[/php]

response2.php
[php]

Response2 <?php if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['location']) ) {
		echo "<h1> Hey {$_POST['name']}, good to see you again! Are you still {$_POST['age']}? And do you still live at {$_POST['location']}?</h1>";
	}

	else {

		echo "<h1> Please fill in all the fields </h1>";

	}

?>

[/php]

response3.php
[php]

Response3 <?php if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['location']) ) {
		echo "<h1>What are you up to, {$_POST['name']}? How's life in {$_POST['location']}? {$_POST['age']} huh, not getting any younger are you?!</h1>";
	}

	else {

		echo "<h1> Please fill in all the fields </h1>";

	}

?>

[/php]

You are my freaking HERO! I love you!! hahaha Yes, I know that is a weird request, but it’s really just a sample of how to use the random responses. Omg, you guys are great! I was going crazy trying to teach myself how to do this. I think I need to just start watching tutorial videos on youtube in the future. But thank you SO much for your help. I’m truly grateful!

Do you have a paypal account that I can make a donation to for your help?

Thanks for the appreciation, I’m glad it helped you.

Regarding PayPal, Thanks but I don’t need it and besides I live in country PayPal doesn’t support. :stuck_out_tongue:

Well, then please accept my thanks. I’m truly grateful. :slight_smile:

You are welcome and if you are beginner and want to learn PHP then I would recommend Kevin Skoglund’s training on “PHP with MySQL Essential Training”. You can find it on lynda.com, be sure to watch the latest version of the training released in June, 2013.

I’ll be sure to check it out! Thanks again! 8)

Hey tanzeelniazi, I wanted to let you know, I tested out your scripts, and of course they work perfectly (I’m sure you knew that part already though) :stuck_out_tongue:

However, I do have one concern. I was able to enter in html tags for bold, and it did infact return the bold characters. That’s bad isn’t it? I mean, bad or me, good for hackers right? lol

Is there a way to strip the tags off so that the user cant inject anything harmful? I read some stuff on htmlentities and strip_tags, but I’m not sure how to apply it to my form being most of the examples I saw are using $string. Any ideas? Thanks again!!

Although You can create a function for cleaning the bad code, but as you are just starting out functions can be intimidating for you therefore here is the code on how to clean out bad stuff. I’ve just updated response1.php please update the others on your own so that you understand what’s going on. ;D

[php]

Response1 <?php if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['location']) ) {
		$name = $_POST['name'];
		$age = $_POST['age'];
		$location = $_POST['location'];

		/* Just re-assigning the safe values back to the variables */
		$name = htmlspecialchars(trim(strip_tags($name)));
		$age = htmlspecialchars(trim(strip_tags($age)));
		$location = htmlspecialchars(trim(strip_tags($location)));

		echo "<h1> Hey {$name}, you are {$age} and you live in {$location}.</h1>";
	}

	else {

		echo "<h1> Please fill in all the fields </h1>";

	}

?>

[/php]

Wow! That was fast! haha

Thank you so much! You’re a scripting-savior!! :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service