PHP & HTML - Getting user input and going to another page [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
NOTE: Possible duplicate question is not helpful, as it does not compensate for multiple options for different pages while also passing form information.
I need to take user input from the current page (for now, this is just a single text field), and there are three buttons. The three buttons are supposed to go to different pages respectively, but all three need the value from the text field to do their job.
Essentially, I need to "pass" the value from the text field to either of the three pages that the buttons should redirect to. Currently, the buttons are unable to redirect as I do not know how to do that.
This is my code thus far:
<html>
<body>
<form name="form" action="" method="post">
Name: <input type="text" name="name" value="<?php echo $name;?>">
</form>
<button type="button">button1</button>
<button type="button">button2</button>
<button type="button">button3</button>
</body>
</html>
No JavaScript please.

<?php
if(isset($_POST['button1'])){
$link='index_1.php?name='.$_POST['name'];
header('location:'.$link);
}elseif(isset($_POST['button2'])){
$link='index_2.php?name='.$_POST['name'];
header('location:'.$link);
} elseif(isset($_POST['button3'])){
$link='index_3.php?name='.$_POST['name'];
header('location:'.$link);
}
?>
<form action="" method="POST">
Name: <input type="text" name="name" value="">
<button type="submit" name="button1" value="button1">button1</button>
<button type="submit" name="button2" value="button2">button2</button>
<button type="submit" name="button3" value="button3">button3</button>
</form>

The page where a form leads to is defined in its action attribute of the formtag. Just write the filepath + filename into that attribute and after submitting, you'll be redirected to that page and can use the POST parameter values there.
But you'll need to add a submit button - one, not three, and as an input tag

Use the submit type, and the check the value for the submitted (or the name you want to put it) in the PHP file, if the value is "button1" do something, if its "button2" etc.
<form action="/action_page.php" method="POST">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<button type="submit" name="submited" value="button1">button1</button>
<button type="submit" name="submited" value="button2">button2</button>
<button type="submit" name="submited" value="button3">button3</button>
</form>

Related

How i can export the action to two differents pages from submit button? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
this is some stupid but i dont know.
How i can export the action to two differents sections from submit button?
I want the cancel button to take me to one page and the accept button to take me to another. How can I apply a conditional that takes different actions?
My buttons are Accept and Cancel...
<section>
<form action="/news">
<input type="submit" id="button" value="Accept"> <input type="submit" class="button2" value="Cancel">
</form>
</section>
<!doctype html>
<html>
<head>
</head>
<body>
<form action="submit.html">
<label for="name">Name</label>
<input type="text" name="name">
<label for="lastname">Last Name</label>
<input type="text" name="lastname">
<button type="submit" id="submit">submit</button>
<button type="cancel" formaction="cancel.html">cancel</button>
</form>
</body>
</html>
Basically with html you can make different action in form like this

How to send data to another PHP file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
There is a href tag:
Send
or a button (needs form tag)
<form action="file.php" method="GET">
<button type="submit">Send</button>
</form>
that will redirect to file.php URL.
I want to send id parameter to another PHP file. How do I do that?
==========================================================================
To send that data where must be provided input params (ex. hidden input) or in-url parameter.
ex.
<form action="file.php" method="GET">
<button type="submit">Send</button>
<input type="hidden" name="id" value="YOUR_ID"
</form>
or
Send
You want to transfer the id only to the next file? Use this in the first:
link_to_2
or
<form...>
<input name="id" type="hidden" value="1">
<button type="submit">btn_to_2</button>
</form>
And in your second file use this for both cases:
<?= $_REQUEST['id'] ?>

How to Prevent Duplicate Form Submissions [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have simple email script with few forms and I am getting duplicate submissions...
So, what is the easiest way to prevent duplicate submissions?
My code..
<form action="main_contact.php" method="post" name="contact_form">
<input type="text" name="contact_name" placeholder="Name">
<input type="email" name="contact_email" placeholder="Email">
<input type="text" name="contact_subject" placeholder="Subject">
<textarea cols="30" name="contact_message" rows="10" placeholder="Your Message"></textarea>
<input type="submit" value="Send">
</form>
<?php
if(isset($_POST['contact_form'])){
var_dump($_POST);
}
?>
You can use jQuery to disable submit button after the form has been submitted:
$('form[name="contact_form"]').submit(function(){
$(this).find('input[type="submit"]').prop('disabled', true);
});
you just need to disable or make visibility hidden after just click on submit button. fist give a id to the submit button
<input type="submit" value="Send" id="sub_id">
and then make it hidden or disable in javascript once you submit the form
document.getElementById('sub_id').style.visibility='hidden';
If you used any framework like Laravel5 or Symfony you would get it "out of the box".
In short:
Register a session token and invalidate it after first form submission.

Post and get method not show correct url [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
<form action="../index.php?option=com_rsform&formId=3" method = "get">
<input type="hidden" name='form[Name]' value="1">
<input type="submit" value="Submit">
</form>
and i need this result:
http://localhost/index.php?option=com_rsform&formId=3&form[Name]=1
but i get this result:
http://localhost/index.php?form%5BName%5D=1
where is the problem?
This seems to be expected behavior regarding form actions when a combination of action URL parameters and form fields are present, and at the moment I'm not finding anything in a spec which tells otherwise.
The practical solution seems to be to just put the values you want in the form itself:
<form action="../index.php" method="get">
<input type="hidden" name='option' value="com_rsform">
<input type="hidden" name='formId' value="3">
<input type="hidden" name='form[Name]' value="1">
<input type="submit" value="Submit">
</form>
Check this: submitting a GET form with query string params and hidden params disappear
The GET parameters of "action" are overwrited by the form. So, the answer of David is correct.
Other solution: make a POST form and keep your URL ;)

PHP Using File_get_Contents() to pre populate a form [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working on a site to help learn some PHP MVC. At the moment the site carries out the CURD commands but I am looking to improve on them.
The items of the database are displayed in on column with two buttons, one to delete and one to update. When the user clicks the update button a form (update_item_form.php) is displayed that will allow the user enter the item information to be update. The form consists of three fields (title, price and description)
What I am trying to do: When the user clicks the update button the form will be pre populated with all the item information connected with that row.
How will can I send the row information to the form when the user clicks the button?
file_get_contents()
$rightBox = file_get_contents( "templates/update_item_form.php" [database value] );
Update_item_form.php
<h2>Update Item!</h2>
<h4>Fill in the form to update an entry.</h4>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='updateItem' />
<p>
<label for="fTitle">Title</label> <input type="text"
id="fTitle" name="fTitle" placeholder="title"
maxlength="25" required />
</p>
<p>
<label for="fPrice">Price</label> <input type="text"
id="fPrice" name="fPrice" placeholder="price"
maxlength="25" required />
</p>
<p>
<label for="fDescription">Description</label> <input type="text"
id="fDescription" name="fDescription" placeholder="description"
maxlength="500" required />
</p>
<p>
<div class="form-group">
<div class="controls">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</p>
</fieldset>
file_get_contents won't parse a PHP file. All you're doing is loading the code into $rightBox, not the output that I assume you are after.
For that, you can use output buffering.
ob_start();
include "templates/update_item_form.php";
$rightBox = ob_get_clean();
This will store output between ob_start() and the ob_get_clean() into $rightBox.

Categories