I am working off a snippet of someone else's code I have found. I would like to update a PHP variable once the select field has changed.
The name of the of the variable is $ProductType as below. The current code does what is expected so now all I'd like to do is set the variable equal to the changed option
<?php
$ProductType = '';
if(isset($_GET['trade'])){
//Everything in here will get echoed in the DIV
echo "You selected: ".$_GET['trade'];
$ProductType = $_GET['trade']; // I'd have thought this might work but when I echo $ProductType, it returns nothing.
exit;
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<form name="x" action="" method="POST">
<select name="trade" onchange="$('#extra').load('?trade='+this.value);">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
</select>
<input type="submit" value="SEARCH" class="submit-button btn" />
</form>
<div id="extra" style="color:red;"></div>
EDIT:
I've added a search button this time. I've echoed it out again and it seems it is working, however, it is returning an array which is not what I am after.
So once the page loads, the user will select from the dropdown and the variable $ProductType should change. If I then submit the form, the page loads again and my variable updates my query.
PHP runs on the server, jquery (javascript) runs inside the browser of the client machine, which bascially means if you inspect the html your php has created for you from inside chrome or IE, all you see is HTML and you php code will be missing, it's missing because the server has run it (on the 'server side') and used it's output to change/append the html you have included in your php.
Your html form uses the 'post' method, so when the http protocol posts (sends back) to the server, you need to use $_POST["trade"] to access the value sent from the browser on postback , php post var documentation here
Usually one adds a hidden variable to one's html form which the php can use as a check, if the form has been posted back to the server, or if no postback was received (first run) then the hidden variable will not appear in the $_POST collection of variables
Related
Firstly apologies. I am a complete novice. I wrote a small database over 10 years ago on whatever versions of php and mysql. I have managed to get the mysql piece working now but what I cant do is pass a variable from a selection page to another page.
I want to choose a location and that variable I assign categorynum as the variable. I then want to hit submit and it open up a second page that will use the value of categorynum in another mysql search. However it doesnt pass over. if i hardcode and declate categorynum as a value it works so I am guessing its the passing from page 1 to page 2
PAGE 1
<form method="get" action="page2.php">
<select name="categorynum">
<?php
foreach ($catByNum as $num=> $name){
print "<option value=\"$name\">$name</option>\n";
}
?>
</select>
<input type="text" name="categorynum" value="Submit">
</form>
Page 2
will pull in the word 'Submit' from the Value= piece (This is the wording that is on the button of the form on page 1 for submitting the choice)
Sorry if its really obvious
In your current page, change the name of your submit button <input type="text" name="categorynumSubmit" value="Submit"> then inside the second page (page2.php), use $_GET["categorynum"] to get the categorynum value
EDITS: Note that type="submit" not text
I would like to have a select list that runs a php database query when an option is selected. I have the code:
<select>
<option value="available">Available</option>
<option value="sold">Sold</option>
</select>
<input type="submit" name="change status">
Say, when a user selects 'sold' I would like to run:
<?php
db_query("UPDATE {product_stock} SET stock='0' WHERE nid='$value'");
?>
I've tried
<option value="sold" <?php db_query("UPDATE {product_stock} SET stock='0' WHERE nid='$value'"); ?> >Sold</option>
but doesn't seem to work.
I don't know if I should be using
<form method="GET">...</form>
I know I can use ajax somehow but I'm really not familiar with it.
Any ideas how to do this?
Thanks
You have to use javascript! Or else you must update your database after form submit.
A possible way with the javascript library jquery would be:
$("select").change(function() {
$.post("/your/url", {option: $(this).val()}, function(return) {
//echo result if necessary
}
});
On server side you should check for $_POST['option'] and update your database after you
ESCAPED
the contents of the variable "option".
You have to send an AJAX call with jquery or plain JavaScript when the combobox got changed.
Depending on the new value, you can send various parameter and call your PHP scripts.
But the interaction between HTML and PHP like you tried will never work. The time you see the combo box in your browser, your PHP commands were already executed.
My site is build on drupal so I created a custom module and used hook_form_alter so I could just use PHP and didn't need to code in ajax.
Well, you will have to create two files one with the form and a second one with the action itself, the first will have something like this.
<form method="POST" action="ActionFile.php">
<input type="text" name="name" id="name" value="">
<input type="submit" value="find name">
</form>
and create a file called ActionFile.php that will contain the variables and the connection to the DB and the select.
What I need is this:
user loads the page (inputs are null)
user gives some input and submits
the inputs have their new values
user navigates back
the inputs have their previous values
My php file looks like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form id="formTest" name="formTest" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" autocomplete="off">
<select id="selectTest" name="selectTest">
<option value=0 <?php if(isset($_POST['selectTest']) && $_POST['selectTest']==0){echo 'selected=\'selected\'';} ?>>Select an option...</option>
<option value=1 <?php if(isset($_POST['selectTest']) && $_POST['selectTest']==1){echo 'selected=\'selected\'';} ?>>Option 1</option>
<option value=2 <?php if(isset($_POST['selectTest']) && $_POST['selectTest']==2){echo 'selected=\'selected\'';} ?>>Option 2</option>
</select>
<br/>
<input id="inputTest" name="inputTest" type="text" value="<?php if(isset($_POST['inputTest'])){echo htmlentities($_POST['inputTest']);} ?>" />
<br/>
<input type="submit" value="OK" />
</form>
<?php
print_r($_POST);
?>
</body>
</html>
As you can see the form "remember" its data after a submit. The problem occurs at this point when the user hits the browser's Back button. Although the POST array has the values of the previous state, the browser fills the fields with the data of the next state (with the ones after the submit).
Although I could managed this problem in Chrome and Firefox with turning off the autocomplete property of the form, but this had no result in IE8.
Any suggestions would be much appreciated!
It sounds like you want the form always to display the values you echoed in your PHP? A quick-and-dirty fix is to reset the form when the page loads:
<body onload="document.forms[0].reset();">
You might need to update that onload attribute for different page structures or you could do it more elegantly if your actual application used jQuery and you give the form an ID.
I believe there is a way to turn off autocomplete/history per-field in IE also. I'd have to google that. Edit: Okay, I did... looks like you have the form attribute IE is looking for. I don't think this is actually an autocomplete issue so much as the back button is trying to recreate the last state of the page... including changes the user made, specifically filling out fields.
Here's the jQuery approach to resetting the form when the document is ready (include back button)... strangely, jQuery hasn't implemented the reset() method of a form, so the [0] gets you the raw DOM element. Add these lines to your head; substitute a local jquery on your server if available and preferred:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(function(){$("#formTest")[0].reset();});
</script>
If you run them through some sort of persistent struct (session scope comes to mind) , you can set that to a conditional on your form. So if it doesn't see the form variable (which is what post is for passing and interrogating form stuff). So the statement would replace the post isset and instead check if the struct 'session' (or some other scope) exists and if so populate. Make sense? I hope this helps.
I have setup a form that allows a user to make selections from drop-down lists, and clicking the submit button will redirect them to the appropriate page based off of their selections. Here is the PHP:
<?PHP
function redirect($where){
header("Location: $where");
}
if ($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/73-prodemo-xp- firefox');
}elseif($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/72-prodemo-xp-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/74-prodemo-win7-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/75-prodemo-win7- firefox');
}
?>
I am manually telling the query where to take the user.
My dilemma is that I want a simple line that says:
"Hi, _. How can I help you?"
The blank would be filled in by the name that is inputted on the form as the Caller's name.
Here is an example of the form:
<form action="" method="post" name="form1">
Caller's Name: <input type="text" name="callersname" /><br />
<select name="os1"> <option selected="selected" value="xp">XP</option>
<option value="win7">Win7</option> </select> <br />
<select name="browser"> <option selected="selected" value="ie8">IE8</option>
<option value="ffx">Firefox</option></select> <br />
<input type="submit" value="Next" />
I'm very new to PHP. If I create a PHP page to post the caller's name, then how do I avoid the page redirecting to that php page instead of the redirect I have manually setup with the drop-down menu options selected?
Thanks for any advixce
If you want to store the user's name on a page other than the first page or the same page that is processing the form values (IE the script that's getting the redirect) you want to store that information either in a database, or more simply in a session.
First, initialize the session on each page where you need to pull that information from:
session_start();
Then, create a session variable (unique to every user/browser) like so:
$_SESSION['user_name'] = $_REQUEST['callersname'];
Now, on the page you wish to retrieve their name (IE Hi ) just do so like this:
echo "Hi {$_SESSION['callersname']} welcome to mcdonalds how may I help you?";
In HTML a form will submit to the url that is given in the value of the action attribute of the form tag. In your example above you have:
action=""
This tells the browser to submit the form to the current page.
It sounds like you are trying to do two somewhat unrelated things in the same step. Are you trying to get the caller's name or have them choose what url to go to? If you want to do both, you may be better off by just accepting the os1 and browser values as POST variables, and dynamically changing the page based on the value of those variables rather than redirecting the user to entirely separate pages.
Try using $_POST instead of $_REQUEST... It may be the problem and too, add action="yourpage.php" to tell the code to send the form to the current page or another page...
I have this simple Select box that is suppose to store the selected value in a hidden input, which can then be used for POST (I am doing it this way to use data from disabled drop down menus)
<body>
<?php
$Z = $_POST[hdn];
?>
<form id="form1" name="form1" method="post" action="test.php">
<select name="whatever" id="whatever" onchange="document.getElementById('hdn').value = this.value">
<option value="1">1Value</option>
<option value="2">2Value</option>
<option value="3">3Value</option>
<option value="4">4Value</option>
</select>
<input type="hidden" name ='hdn' id="hdn" />
<input type="submit" id='submit' />
<?php
echo "<p>".$Z."</p>";
?>
</form>
</body>
The echo call works for the last 3 options (2,3,4) but if I select the first one it doesnt output anything, and even if i change first one it still doesnt output anything.
Can someone explain to me whats going on, I think it might be a syntax issue.
EDIT: As mentioned in first paragraph: I am using the hidden field instead of just using the value of selected because i plan on disabling the select drop down in my actual website, and when you disable the menu the POST data is no longer accessible. By putting it into a hidden field you can still access the information
The first one is the default, so when you "change" to the first one, it hasn't actually changed and the event does not fire.
You should be reading the value directly from the select and not depending on JS to set a hidden field though. The JS is just pointless, unreliable complexity.
Does anything else in your client-side application depend on that hidden field? If the ONLY purpose of the hidden field is to duplicate the selected value of the dropdown, then there's absolutely no reason to use an onchange handler.
Best solution would be to set an onsubmit handler on the form itself to copy the dropdown value into the hidden field.
<form ... onsubmit="document.getElementById('hdn').value = document.getElementById('whatever').value">
Try your post access like this:
<?php
if (array_key_exists('hdn', $_POST)) {
$Z = $_POST['hdn'];
}
else {
$Z = 1;
}
?>
change your input:
<input type="hidden" name='hdn' id="hdn" value= <?php echo "$Z"; ?> />
this.value has no value. That is why $_POST['hdn'] doesn't have a value when you initially load the form.
As #David said, if you use Firefox you can see the post data for hdn is empty/null.