How to change form from get to post? - php

the links of this form have question marks:
This is a file: sidebar.php
echo
<form method='get'>
<div class='left'>
<div class='btn-group-vertical'>
<!-- Vertically Stck button group -->
<button 'submit' name='Service)name' class='btn btn-default'>Service Name</button>
<button type='submit' name='YYY_Service' class='btn btn-default'>YYY Service</button>
</div>
</form>;
?>
This is a part of index.php:
ini_set('session.save_path','/path/to/session');
session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$current_menu='empty.php'; //If no menu/button is selected get right column empty(empty.php)
if(isset($_REQUEST['Service'])){$current_menu='serviceform.php';}
if(isset($_REQUEST['YYY_Service'])){$current_menu='yyyform.php';}
This is showing URLS such as:
www.foo.com/index.php?Service=
www.foo.com/index.php?YYY_Service=
How should this form changed in order to show instead urls this way?
www.foo.com/serviceform.php
www.foo.com/yyyform.php
And this code in another file:
function Button_set($name, $newline)
{
if($newline){
?>
<br><br><input type="button" name="<?=$name?>" value="<?=$name?>" /input>
<?php
}
else if (!$newline){
?>
<input type="button" name="<?=$name?>" value="<?=$name?>" /input>
<?php
}
}
If I only change the form method to POST it doesn't work...

Change:
<form method="get">
to:
<form method="post">
Also add attribute "action" to site you want:
<form method="post" action="serviceform.php">
<form method="post" action="yyyform.php">
W3C link on form

Related

delete button in PHP website behaving like static when clicked

i have a simple delete button in PHP which uses simple SQL query to delete the data from database, I have used the following code:
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}
<div style="margin-left:38%" class="clearfix">
<a href="https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory">
<button type="button" class="cancelbtn bemine">Cancel</button></a>
<form action="" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
the problem here is if I click the delete button, its not responding, nothing is happening. the button is like static. can anyone please tell me what could be wrong here
If any Javasscript is not involved, than just put form action.
Also send myid in the request or in URI.
please see below:
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="abc.php" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
You should add an action url/path to where your PHP file is located and add a Method to your form. Also, I noticed you deleting an entry where "name" is equal to $myid please check if that is correct.
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="url-to-server.php" method="post" >
<button type="submit" name="submit" class="deletebtn bemine">Delete</button>
</form>
</div>
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}

How to keep values in re-opened form?

I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?
Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

save input data in php to notepad.txt

<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.
The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help

Printing html page and doing some action using 1 button

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?
If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>
you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Make sure you are using input type button inside the form tag. And use post method for form.

Submit form does not work correctly

<div class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<?php echo form_open('login/validate_credentials');?>
<?php $u = 'placeholder="Username"';
$p = 'placeholder="Password"';?>
<?php echo form_input('username','',$u,'class="input-block-level"');?>
<?php echo form_password('password','',$p,'class="input-block-level"');?>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<?php echo form_submit('submit','Sign in','class= "btn btn-primary"');?>
<?php echo anchor('login/signup','Sign up!', 'class= "btn btn-primary"');?>.<br/><br />
<?php echo anchor('login/admin_log','Go to admin login page');?>
<?php echo form_close();?>
</form>
</div>
I have a login form. When I click sign , it's not redirecting me to the form_open page.
you need to create an action for the form, usually a php script on a different page to handle the data:
as an example:
<form action= "../create_comment.php" method="post" name="comments_form" id="comment" enctype="multipart/form-data">
<div>
<label>Name<span>*</span></label>
<input name="name" type="text" value=" ">
</div>
</form>
You have two forms Parent and child(form inside form).
Submitting the form will process parent form. Simply remove the first (parent) <form> tag.
<form class="form-signin">
^^^^^^^^^^^^^^^^^^^^^^^^^^ ------ remove this
...
...
</form>
^^^^^^^ ------ and this
It looks like you might be using CodeIgniter?
You have 2 form tags in your code.
here: <form class="form-signin">...</form>
and here:
<?php echo form_open('login/validate_credentials');?>...<?php echo form_close();?>
Get rid of this one: <form class="form-signin">...</form>
Your second form tag will handle everything for you. The output will look something like this:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/login/validate_credentials" />
If you need to add a class or any other property to the form tag, do this:
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
Form helper on CI Docs

Categories