On my form I have 3 submit,I want to know the best way to handle these buttons because the default action is the same form:
<form method="POST" action="file_where_form_is.php">
Name:<input type='text' name='name'>
<input type='submit' name='add' value='Add Names' ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit'name='Preview'value='Preview'ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit' name='submit'value='Submit' ONCLICK='window.location.href="send.php">
</form>
This not seems to work completely,just the 2 first work and for the last submit does nothing(with name='submit')
Any suggestions?
I finally found in header part add something like this:
if(isset($_POST['submit'])){
header("location: somefile.php");
}
elseif(isset($_POST['Preview'])){
header("location: anotherfile.php");
}
elseif(isset($_POST['add'])){
header("location: anotheronefile.php");
}
Related
I always have this error when changing any form in my site from post to get and click on submit button redirecting me to index.php.
<form action="index.php?pg=users" method="get">
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit ' name='submit ' value='Search'>
</form>
You might want to update your form a little,
For POST, doing this is fine :
<form action="index.php?pg=users" method="post">
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit' name='submit' value='Search'>
</form>
But for GET, in your form action you have "index.php?pg=users" where "pg=users" is part of your URL query string already.
You can move the "pg=users" as part your form input with type hidden like the example below :
<form action="index.php" method="get">
<input type='hidden' name='pg' value='users'>
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit' name='submit' value='Search'>
</form>
Remember that in method="GET", your input will be appended to your action URL on form submit and will ignore the query string in action URL.
I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.
This is a simple demonstration of my code :
if(isset($_POST['login'])) {
do this
}
<form method='POST' action="index.php">
Username : <input type='text' name='username'/>
Password : <input type='password' name='password' />
<input type='image' src='images/login.png' name='login' value='login'/>
</form>
The problem is that , when i use type='image' as a submit button, in Firefox and IE nothing happends. But if i use type='submit' everything works fine..
Well i dont want to display the button as a submit one, but as an image, so what im doing wrong ?
Simply change your if to look for any other field in the POST. Image type button wont send that value.
Change:
if(isset($_POST['login'])) {
To
if(isset($_POST['username'])) {
If you want to check login, you need to check for login_x or login_y values instead of login, which is not created when button is an image.
<?php
if(isset($_POST['login_x'])) {
echo "Here I m";
}
?>
<form method='POST' action="index.php">
Username : <input type='text' name='username'/>
Password : <input type='password' name='password' />
<input type='image' src='images/login.png' name='login' value='login'/>
</form>
Sorry, I explained in a comment the type=image creating _x and _y, but I forgot the params login does not exists in that case
On my website i got a form looks like this on index.html:
<form id="demo" action='submit.php' method='post' enctype='text/plain'>
link: <input type='text' name='web'></br>
<input type='submit' value='submit'>
</form>
And then in the submit.php i got:
<?php
$fp=fopen('sub.txt','a');
fwrite($fp,addslashes($web));
fclose($fp);
header('location: thanks.html');
exit();
?>
But when i press submit the result in sub.php is beeing
/n
But it should be
example.com/n
What is wrong in the php code.
I want the link submited in the form printed in the file sub.txt and then the person be redirected to a thank you page.
You missing value attribute as well as you need $_POST to get the value inside sub.php
<form id="demo" action='submit.php' method='post' enctype='text/plain'>
link: <input type='text' name='web' value="" /></br>
<input type='submit' value='submit' />
</form>
<?php
$fp=fopen('sub.txt','a');
fwrite($fp,addslashes($_POST['web']). "\r\n");
fclose($fp);
header('location: thanks.html');
exit();
?>
I'm NOT using javascript has it is disabled in my environment.
Please review the code below.
I'm trying to use PHP to create the logic that Javascript or jQuery would allow me to do with a simple : document.form2.submit()
<div>
<h2>How many services do you need ?</h2>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST" name="fServ">
<input type="number" name="numServ" />
<input type="submit" value="SEND">
</form>
<div>
<?php
if(isset($_POST['numServ']) && $_POST['numServ']!==""){
echo "We need ".$_POST['numServ']." services";
echo "<form name='form2' id='form2' method='get' action= $_SERVER[PHP_SELF]>";
for($k = 0 ; $k< $_POST['numServ']; $k++){
//echo "<input type=\"text\" value=\"services$k\">";}
echo "<br><input type='text' name='services$k' value=''>";
if(empty($_GET['services'.$k])){
echo "You didn't fill up all the fields. Please put in a service";
}
if (!empty($_GET['services'.$k])){
echo "Form Filled";//
}
}
}
echo "<input type='submit' name='sendForm' value='SEND'/></form>";
if(!isset($_POST['numServ'])){
echo "We don't have any services yet.";
}
else if($_POST['numServ']==""){
echo "Please put in a number";
}
?>
Can this be achievied through PHP ? Where if the conditions are met and the form fields are NOT blank then submit the form, otherwise die and show a message.
Even then you need to submit the details of the form:
Name: <input type="text" name="name" /> (Min 5 Chars)
Age: <input type="text" name="age" /> (Should be a Number)
<input type="submit" />
Now using server-side validation, this can be done this way:
if (isset($_POST["name"], $_POST["age"]))
if (strlen($_POST["name"]) > 5 && is_numeric($_POST["name"]))
die("Error! The conditions are not met!");
die("Form Submitted!");
This way you can submit the form where JavaScript is disabled.
If I understand, you need auto-submit the form without javascript.
Maybe this thread helps you:
How to submit a form on page load without clicking submit button?