Query execution php script in the same page - php

I have this form html code in update.php. For updating, it's required to link to another page save_seeker.php with mysql update script for it to be executed. Is there any way to execute the script in same page on submitting form so that after query execution it remains on same page?
<form action= "save_seeker.php" method = "post">
Update details !<br><br>
First Name
<input type = "text" name = "fname" value = "<?php echo $disp['fname'];?>"><br><br>
Last Name
<input type = "text" name = "lname" value = "<?php echo $disp['lname'];?>"><br><br>
Contact number
<input type = "text" name = "contact" value = "<?php echo $disp['contact'];?>"><br><br>
Email-id
<input type = "email" name = "email" value = "<?php echo $disp['email'];?>"><br><br>
Address
<input type = "text" name = "address" value = "<?php echo $disp['address'];?>"><br><br>
Experience
<input type = "number" name = "experience" value = "<?php echo $disp['experience'];?> "><br><br>
Qualification
<input type = "text" name = "qualification" value = "<?php echo $disp['qualification'];?>"><br><br>
<input type = "Submit" value = "Update">
</form>

You could do the processing in the same file by adding this into the form action.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<!-- Input fields in here-->
<input type="submit" name="form_submit" value="Submit">
</form>
Now check for the submit action by checking if it is set in the post variable
<?php
if ( isset( $_POST['form_submit'] ) ) {
// Do processing here.
}
?>

You could use the include('page-with-update.php') , call the update function and use the $_SERVER["PHP_SELF"].
I hope helped!!

Related

Generic PHP form processor

I'm trying to create an elegant form processor that can receive POST OR GET and cycle thru the values and display them. I'd like to make it as simple/elegant as possible, and can't seem to eliminate GET/POST in favor of REQUEST. This code outputs an extra value pair, and I clearly don't understand the relationship of GET/POST to REQUEST. The form includes previous GET information when POST is used (so they seem to overlap) and I'm not sure how to easily NOT include the cookie (I think that's what it is) value. I understand REQUEST is not the best way to do this, but wondering still if there's not a cool way to use it in this fashion. Open to other suggestions.
<h1>
Welcome to the Flexiform! AKA "The Formerator"
</h1>
<p>
This form should process any combination of inputs... this iteration doesn't handle identically-named inputs other than checkboxes. Would like to figure that out.
</p>
<?php
if ($_GET || $_POST)
//if ($_REQUEST) // why can't I use this?... it seems to process the incoming page as GET on firstload...
{
echo ("<h2>Way to '" . $_SERVER['REQUEST_METHOD'] . "' some! </h2>");
foreach($_REQUEST as $submittedName => $submittedValue)
{
if (is_array($submittedValue)) // for checkboxes with more than one selected value
{
$submittedValue = implode(', ', $submittedValue);
}
echo "For the <b>" . $submittedName . " input, you submitted: :</b> " . $submittedValue."</br>";
}
}
else
{
echo ("<h2>Please submit one of the two forms to see some results.</h2>");
}
?>
<hr><hr>
<br>
GET SOME!
<form action= "" method = "get" >
<input type = "text" name = "name" value = "GIcabad" />
<input type = "text" name = "name2" value = "GIcabad2" />
<input type = "text" name = "name3" value = "GIcabad3" />
<input type = "text" name = "name" value = "GIcabadbb" />
<input type = "text" name = "name2" value = "GIcabad2bb" />
<input type = "text" name = "name3" value = "GIcabad3bb" />
orange:<input type = "checkbox" name = "colors[]" value = "orange" checked />
red:<input type = "checkbox" name = "colors[]" value = "red" />
pink:<input type = "checkbox" name = "colors[]" value = "pink" checked />
<input type = "submit" />
</form>
<br>
POST SOME!
<form action = "" method= "post" >
<input type = "text" name = "name" value = "PIcabad" />
<input type = "text" name = "name2" value = "PIcabad2" />
<input type = "text" name = "name3" value = "PIcabad3" />
<input type = "submit" />
</form>
You can loop it once on the main file of your application and store the $_GET and $_POST inside a single variable ( It will act like $_REQUEST but w/o $_COOKIES ). Then you can use it like this
<?php
$input_values = array();
$gpcs = array_merge($_POST, $_GET);
foreach ($gpcs as $key => $value) {
$input_values[$key] = $value;
}
// Then you can use anywhere in your application like this
echo isset($input_values['username'])? $input_values['username']:'';
echo isset($input_values['password'])? $input_values['password']:'';
echo isset($input_values['user_type'])? $input_values['user_type']:'';
?>
Edit: Form and Result
<form action="" method="POST">
<input type="text" name="demo" value="Demo"><br/>
<input type="text" name="demo2" value="Demo 2"><br/>
<input type="text" name="array[]" value="Array 1"><br/>
<input type="text" name="array[]" value="Array 2"><br/>
<input type="text" name="array[]" value="Array 3"><br/>
<input type="text" name="array[]" value="Array 4"><br/>
<input type="submit">
</form>
Array
(
[demo] => Demo
[demo2] => Demo 2
[array] => Array
(
[0] => Array 1
[1] => Array 2
[2] => Array 3
[3] => Array 4
)
)

Unable to delete mysql data from php

I have successfully connected to database and published mysql data on web and manually added joke on it.Screenshot of what output looks like is provided here.
When i manually enter the corresponding id of joke inside the textbox and press delete button, it deletes the joke corresponding to that it. But when i click on the delete button directly, that joke isn't deleted. How to update value of that textbox ? For the time being, i have made input type of textbox as text so to see whats going inside it. I am using PHP version: 7.1.1 and Apache/2.4.25 (Win32.
<p>Add your own joke</p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke) : ?>
<form action ="?deletejoke" method='post'>
<blockquote>
<p><?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8');?>
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
<input type = 'submit' value = 'Delete'>
</p>
</blockquote>
</form>
Screenshot of Mysql database is provided here.
there is a space between <? and php on the input value it should be <?php;
then it should work.
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
<input type="text" name="id" value="<?php echo $joke['id'];?> ">
you should remove the spaces (not just in this line)
Change
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
to
<input type = "text" name = "id" value = "<?php echo $joke['id']; ?>">

Sign In won't Work

I have created a basic sign in form:
<form action = "" type = "post">
<input type = "text" name = "txt_user" placeholder = "Username" required/>
<input type = "password" name = "txt_pass" placeholder = "Password" required/>
<input type = "submit" name = "btn_submit" value = " Sign In ">
and posted this above my html:
<?php
if(isset($_POST['btn_submit'])) {
$username = $_POST['txt_user'];
echo $username;
?>
to see if the form works, it doesn't. The txt_user won't display up top the html as it should be but instead change my url from this:
localhost:8080/tryouts
to this:
http://localhost:8080/tryout/?si_username=asd&si_password=asdasd&si_submit=++Sign+In++
can you tell me what's going on? I tried this method before and it works. Maybe I misplaced something?
Update
The page containing the form is called signin.php where it's been placed inside index.php like this:
<body>
<?php include("signin.php"); ?>
</body>
purpose of this is to save time and space for the php codes so that I won't be bother changing the sign in form from page to page, instead just changed the signin page main php page. I don't think it's causing this but still...
You need to set your form method to POST; form type is invalid. Right now, it's defaulting to using GET.
<form action = "" method = "post">
<input type = "text" name = "txt_user" placeholder = "Username" required/>
<input type = "password" name = "txt_pass" placeholder = "Password" required/>
<input type = "submit" name = "btn_submit" value = " Sign In ">

pass form data and a variable (ID) to a controller function codeigniter

I want to pass a variable(id) along with form data to a controller function. the code is as folows
<form action = "index.php/my_controller/my_function" method = "POST">
<input name = "comment" type = "text" size = "8">
<input type = "submit" value = "save">
</form>
<?php
my_function($id){
// get the text
// get the id
//save id and text
}
?>
when I try
`action = "index.php/my_controller/my_function/<?php echo record['ID']?>"`
where $record['id'] gives me the id, the URL shows the id but HOW do i pass it to my_function($id) that is in the controller file.
Try using hidden field
<form action = "index.php/my_controller/my_function" method = "POST">
<input name = "id" type = "hidden" value="<?php echo $id; ?>">
<input name = "comment" type = "text" size = "8">
<input type = "submit" value = "save">
</form>

Adding a link to a HTML submit button

I'm just getting into mysql and i am creating a basic registration page. However, when i click submit, i want to be directed to another page, say a page where you login, as many sites have.
How do i go about allowing the submit button to link to a new page. I know it'll be something simple, but i have looked and can't seem to find much on it.
This is the current code that i've tried:
<html>
<head>
<title>MySQL Test - Registration</title>
<script LANGUAGE="JavaScript">
function testResults() {
window.document.location.href = "http://www.google.com";
}
</script>
</head>
<body>
<form action = "rego.php" method = "POST">
Username:<br/>
<input type = "text" name = "username"/><br/>
Password:<br/>
<input type = "password" name = "password"/><br/>
Confirm Password:<br/>
<input type = "password" name = "passvery"/><br/>
Email:<br/>
<input type = "text" name = "email"/><br/><br/>
Please select your gender:<br/>
<input type = "radio" name = "gender" value = "male"/>Male &nbsp&nbsp&nbsp&nbsp&nbsp
<input type = "radio" name = "gender" value = "female"/>Female<br/><br/>
<input type = "checkbox" name = "agree" value = "agree"/>I agree to the terms and conditions.<br/>
<br/>
<input type = "submit" value = "Sign Up!" onclick = "javascript:testresults()" />
</form>
</body>
</html>
Form action="rego.php" means that your form will be submitted to that file.
If you want to go somewhere else after rego.php, you should redirect from that file, for example using the location header, before any output:
// do stuff (registration)
header("Location: another-php.php");

Categories