Jump to content

php if statement in html form


Go to solution Solved by Barand,

Recommended Posts

I have a comment system that runs on ajax. There is an html form that has a field for name, email, and comment. Then there is a submit button. I also have a button that gives you the option to log in. Here is the code for that:

<div id="addCommentContainer">
	<div id="login"><input type="submit" id="submit" value="Or Login" onclick="window.location='/LoginScripts/login.php'" /></div>
	<p>Add a Comment</p>
	<form id="addCommentForm" method="post" action="">
		<label for="name">Your Name</label>
		<input type="text" name="name" id="name" />	
		<label for="email">Your Email</label>
		<input type="text" name="email" id="email" />   
        <label for="body">Comment Body</label>
        <textarea name="body" id="body" cols="20" rows="5"></textarea>
        <input type="submit" id="submit" value="Submit" />
    </form>
</div>

If a user chooses to log in, then they would no longer need to type their name and email in each time they want to comment. So I was going to put a PHP if-statement around the name, email, and login button. This way they will not display if the user is logged in. I would then use another if-statement to redefine their name and email that is sent to the ajax. Something like this:

<div id="addCommentContainer">
	<?php if(!$username && !$userid) : ?>  
	<div id="login"><input type="submit" id="submit" value="Or Login" onclick="window.location='/LoginScripts/login.php'" /></div>
	<p>Add a Comment</p>
	<form id="addCommentForm" method="post" action="">
		<label for="name">Your Name</label>
		<input type="text" name="name" id="name" />	
		<label for="email">Your Email</label>
		<input type="text" name="email" id="email" />  
	<?php endif; ?>
	
	<?php if($username && $userid) : ?>  
		// redefine name and email that is sent to ajax. Some kind of mysql_query that will grab it from the DB of users.
	<?php endif; ?>	  
        <label for="body">Comment Body</label>
        <textarea name="body" id="body" cols="20" rows="5"></textarea>
        <input type="submit" id="submit" value="Submit" />
    </form>
</div>

I am very confused on how to pull this off. The code requires that name, email, and comment are filled before you can press the submit button. So I somehow need to redefine those values using a mysql_query for logged in users before they press the submit button. 

 

If anyone has any input on how to do this, that would be amazing!

Link to comment
https://forums.phpfreaks.com/topic/284791-php-if-statement-in-html-form/
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.