Set a SESSION by clicking a radio button in PHP? - php

I have a Form on my site and part of the Form allows the user to select a colour of car.
I'd like it so once they select a radio button, it updates a session variable dynamically - using Ajax & PHP.
HTML Form:
<form method="post">
red: <input type="radio" name="car" value="red"><br />
blue: <input type="radio" name="car" value="blue">
</form>
PHP Script:
session_start();
$carColour = $_POST["car"] ;
but I'm not sure how to do this with Ajax so it doesn't leave the page.
Is this possible - and how would I achieve this?
Many thanks for any help.

You wouldn’t be able to do it with PHP alone, as it’s not an event-driven language. By the time you click the radio button, the PHP interpreter has been and gone.
You’ll need to use JavaScript function to call a PHP script that sets the session, and attach that function to the onclick event of your radio button.

Related

How to add a custom button (image) in an onclick event?

I'd like some help, because I am trying to learn AJAX and I'm stuck. So I have this code here and everything is right, when i press the buttons the information from the PHP file are displayed perfectly.
`<form>
<input type="button" value="req" onclick="fetch('hotels.php?select=1')">
<input type="button" value="req2" onclick="fetch2('hotels.php?select=2')">
<input type="button" value="req3" onclick="fetch3('hotels.php?select=3')">
</form>`
So what I needed to ask is the following: Can I replace the plain classic onclick button with a custom one that I've made in Da Button Factory? I have tried to change the input type to an image, but the problem is that the page is refreshing, when I click it (On the other hand when I just have the classic button onclick it doesn't refresh and the infos are displayed). My programming teacher told me that it must not refresh, as we are working on AJAX right now. Here is what I've tried to do
<input type="image" src="button_london.png" alt="randomtext" value="req2" onclick="fetch2('hotels.php?select=2')">
Any tips?
Input image by default acts as a submit button. If the input type image does the job for you, simply add return false after your onclick function to prevent refreshing.
<input type="image" value="req" onclick="fetch('hotels.php?select=1');return false;">

How can a submit button made to be act as a link too?

I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);

Run a Function From a Form

I want to add a function to a form, so when the submit button is clicked, a function is executed instead of leaving the page.
And I can not use $_POST, because I need the url.
Here is my script :
function submitclicked()
{
echo 'Hello World';
}
<form action="" method="post" id="TransactionAddForm">
<input type="hidden" id="TransactionAccountID" value="'.$zpid.'" name="data[Transaction][account_id]">
<input type="hidden" id="TransactionAmount" value="'.$_POST['price'].'" name="data[Transaction][amount]">
<input type="hidden" id="TransactionDesc" value="'.$desc.'" name="data[Transaction][desc]">
<input type="hidden" id="TransactionRedirectUrl" value="'.$backurl.'" name="data[Transaction][redirect_url]">
<div class="submit"><input type="image" src="http://www.zarinpal.com/img/merchant/merchant-6.png" ></div>
</form>;
Tnx for your help.
Your basic understanding is flawed. PHP is a server side language, meaning, it runs on the server side. It cannot capture button clicks or browser events. It can only work with requests from the client to the server.
Your function may only run after the form was submitted to the server.
For your problem, use JavaScript.
Also, what you have here is not a form. The user has no form controls to choose from here.
Add a return false; at the end of the function you call on submit. This way, the form will not be submitted. You can also add a simple button, which is the type button, and not the type submit. Those buttons will also not submit the form.
-- EDIT --
I am assuming you want to call a JavaScript function on the click of your submit button. any PHP is of course server-side...

HTML Javascript forms, and a php script - Simple question, help! what does this code do?

So I have this simple form:
<form action="includes/process.php" method="post" name="standard_use" id="standard_use" enctype="multipart/form-data">
<button onclick="dofunction(); return false;">Do it!</button>
<input type="file" id="upload_file" name="filename" style="float:left;width:70%;" size="42"/>
</form>
So what happens really when the button is clicked ?
Is it that the php file is called ? does it not ? the javascript is called before ?
Anyone can shed some light on this ?
Thanks !
Well, when you hit the button the following events occurs:
You send a REQUEST to the server
The php codes evaluates the request and runs some codes
Finally it returns back a RESPONSE which you see as a web page
Javascript is a client-side script which means that whenever you make an action on the page, the code runs. For instance, when you click the button, before sending the request javascript will work. You may, for instance, place a function that will be triggered when you hit the button which checks the form and either approves the form or shows the error messages
EDIT
As far as your comment is concerned:
Yes, javascript runs first when you hit the submit button. Php runs only when you submit the form and make a request to the server.
Consider this example: (I am better at explaining things with examples:)
<form action="somepage.php" onsubmit="return checkMe()" method="POST">
<input name="firstname" id="fn" value="" type="text" />
<input type="submit" value="Submit" />
<script type="text/javascript">
function checkMe(){
var tb = document.getElementById("fn")
if(tb.value == "Alex") return true;
else return false;
}
</script>
</form>
So basically, when you hit the button and try to submit the form, the javascript will first check whether the name provided in the textbox is Alex or not, if it is not then it will not submit the form. If it is Alex then it will submit the form and then the form will redirect the user to somepage.php. Finally, the php codes will work in somepage.php and the page will be rendered again.
What happens is that only doFunction() javascript function is invoked and nothing more.
However, it might be possible that this javascript function invokes "submit" event on the form and the request is sent (what you described as "php file is called").
Your code just trigger javascript event and your function. To submit a form you need an
<input type="submit" value="Submit" />
or a button, which default type is submit (thx davin)
<button value="Submit" />
However as far as you return false in your javascript code your form won't be submitted even with the submit button.

radio value lost by $_post array

I have this simple form:
<form method="post" action="?step=2">
<label>A4 - Colour / Colour
<input type="radio" name="leaflet" value="1"></label><br>
<label>A5 - Colour / Black
<input type="radio" name="leaflet" value="2"></label><br>
<input type="submit" name="leaflet" value="Select">
</form>
When I apply print_r ($_POST); to the submission though, I only get the submit button data. I don't even see the radio name.
What could do that?
PHP's standard form parsing system (which populates $_POST) can't handle multiple bits of form data with the same name (unless that name ends with the characters []).
Change the name of the submit button.
Buddy you have same name for submit button and radio button :)
ie leaflet
The value get overridden. I HOPE u got it

Categories