Form keeps sending me to the wrong section after submit

Hi, this is my code. I have created a form in the section “form_page”, but everytime I submit it sends me back to the main page with this URL: http://localhost/seminarka/index.php?send_form=Odeslat
I’ve been trying to figure it out, but I don’t know where I am making the mistake, could someone help me? Thanks.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<?php 
	if(isset($_REQUEST["section"])){
		$section = $_REQUEST["section"];
	}
	else{
		$section = "";
	}
	
	if ($section == ""){$title = "Main Page";}
	if ($section == "form_page"){$title = "RegForm";}
	
	
	
	?>
<title><?php print($title) ?></title>
</head>

<body>
	<header>
		<ul id="menu">
			<li><a href="index.php">Main Page</a></li>
			<li><a href="index.php?section=form_page">RegForm</a></li>
		</ul>
	</header>
	<main>
		<div id="main_content">
			<?php if ($section == ""){ ?> 
				<h1>O klubu</h1>
				<p>...blablabla...</p>
			<?php } ?>
			
			<?php if ($section == "form_page"){ ?>  
				<h1>RegForm</h1>
				<form action="" method="get">
					<label for="name_input">Jméno: 
						<input type="text" id="name_input">
					</label><br>
					<label for="surname_input">Příjmení: 
						<input type="text" id="surname_input">
					</label><br>
					<label for="username_input">Uživatelské jméno:  
						<input type="text" id="username_input">
					</label><br>
					<label for="passwd_input">Heslo: 
						<input type="password" id="passwd_input">
					</label><br>
					<label for="passwd_verif_input">Ověření hesla: 
						<input type="password" id="passwd_verif_input">
					</label><br>
					<input type="submit" name="send_form">
				</form>
			<?php } ?>
			
		</div>
		
	</main>
	<footer></footer>
</body>
</html>

Why are you using GET instead of POST? What are you actually trying to accomplish with this code?

2 Likes

In this form opening tag

<form action="" method="get">

you don’t specify anything for the action parameter, so it will reload the page you are on when you submit the form. If you want it to load a different page on submit, add that page URL into the parameter.

I tried this, but it still does the same thing

<form action="index.php?section=form_page" method="get">

If that code you posted is called index.php, then it will send you to the same place. The only difference is that it’s now set a variable called $_GET['section'] with a value of “form_page”.

Back to the question by @benanamen, what are you actually trying to do here? And is the code you posted index.php, or something else?

Yea, the file is called index.php - my goal right now is to stay on the same page after I submit the form and after that something like

<?php if (isset($_REQUEST["send_form"])){
				print(something...)
			} ?>

If you want to actually stay on the same page without a refresh, you’ll need to use something like Ajax to submit the form. Using the standard submit it would re-load the page with the various form variables set. I would look at using $_POST instead of $_GET and $_REQUEST, and pass your “section” as a hidden form variable instead of as part of the URL.

Keep in mind that your logic re-draws the form if that variable is set, so if you want to come back to the same page but not draw the form, you need to swap that around.

Is making more .php files also a solution? What I have in mind is creating separate php files for each section (main page, gallery, reg form, …)

Yes. But you want to include those files into the index page as needed. A single point of entry is a good setup to have. Make sure to include them dynamically, not with a bunch of if else clauses

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.