POST method is not working on same page PHP - php

I know this is asked question however I have tried almost steps but its not working. I know there is a silly mistake somewhere need an experts eye.
My code:
// if(isset($_POST["country"]))
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$country = $_POST["country"];
echo '<script language="javascript">';
echo "alert(' Officer Already Alloted..!!!');";///Tried getting alert once POST, but no message
echo '</script>';
}
//? $country = $_POST["country"] : $company=1;
?>
<form action="#" method="POST">
<select class="country" name="wcpbc-manual-country" id="country" >
<?
$list=mysqli_query($con,"select * from country where status!='False'");
while($row_list=mysqli_fetch_assoc($list)){
$display="+".$row_list['phonecode']."-".$row_list['name'];
$flag=$row_list['isosmall'];
?>
<!--<select class="country" name="wcpbc-manual-country" id="country">-->
<option value="<?$row_list['phonecode']?>" data-iconurl="https://ipdata.co/flags/<?php echo $flag; ?>.png" <?php if($country==$row_list['name']){echo "selected";} ?>><? echo $display;?></option>
<!--<option value="IN" data-iconurl="https://ipdata.co/flags/in.png">IN some text</option>-->
<?
}
?>
</select>
</form>
<script type="text/javascript">
$("#country").selectBoxIt();
</script>
Actually I am trying to get selected value on to dropdown, it seems POST is not working for assigning value to $country variable
I tried:
1. if($_SERVER['REQUEST_METHOD'] == "POST")
2. used form action="<?php echo $_SERVER['PHP_SELF']; ?>"
Please help.Thanks

The <option /> values are empty:
<?$row_list['phonecode']?>
Should be
<?php echo $row_list['phonecode']; ?>
or at least
<?= $row_list['phonecode']; ?>
Also as mentioned by Always Sunny you are not visibly submitting the form. We don't know if there is Javascript going on but there must be some kind of submit action. I think you checked that in the browsers developer console.

you should use
<select class="country" name="country" id="country" >
<option value="<?php echo $row_list['phonecode']?>" data-iconurl="https://ipdata.co/flags/<?php echo $flag; ?>.png" <?php if($country==$row_list['name']){echo "selected";} ?>><? echo $display;?></option>

Related

Passing an input value to a select option to another page using POST

I have an input text from form1.php that I want to pass to form2.php that has a select option, and i'm trying to get whatever the input text from form1.php to be the selected option in form2.php.
It looks straight forward when trying to pass a select value to an input, but i'm stuck when i turn things around.
form1.php:
<form class="form1" action="form2.php" method="POST">
<input type="text" name="inputval">
<button type="submit" name="submit">Submit</button>
</form>
form2.php:
<?php
if (isset($_POST["submit"]))
{
<select name="productselect" id="productselect">
<option>Car</option>
<option>Bus</option>
<option>Plane</option>
<option>Sail Boat</option>
</select>
}
else{}
?>
Any help in pointing me in the right direction would really be appreciated. Thank you.
You need to check in each select option if the value of $_POST['inputVal'] is the same as the option, and if so add selected="selected" to mark it as selected option.
Also take into account that if no option is marked as selected, the first one will be selected automatically.
<?php
if (isset($_POST["submit"])) {
?>
<select name="productselect" id="productselect" >
<option <?php if($_POST['inputVal'] == 'Car') echo 'selected="selected"' ?>> Car</option >
<option <?php if($_POST['inputVal'] == 'Bus') echo 'selected="selected"' ?>> Bus</option >
<option <?php if($_POST['inputVal'] == 'Plane') echo 'selected="selected"' ?>> Plane</option >
<option <?php if($_POST['inputVal'] == 'Sail Boat') echo 'selected="selected"' ?>> Sail Boat </option >
</select >
<?php
} else {
}

Select element - Post method - return of value / Php

I'm having a hard time to fix and how can make my codes work well.
My textbox echo correctly while my dropdown box is not.
Can anyone help me and also clean my codes?
I wanna know how did you do it and can u please explain it to me.
Thank you so much.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Dropdown: <select name="drpdown1" value="<?php echo $drpdown1;?>">
<option></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
</select>
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$drpdown1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$drpdown1 = $_POST["drpdown1"];
}
?>
You're not echoing the value of $drpdown1 correctly:
// this is wrong for a select:
<select name="drpdown1" value="<?php echo $drpdown1;?>">
// etc.
If you want to select automatically the previously selected value, you need to add the selected attribute:
<select name="drpdown1">
<option value="1" <?php if ($drpdown1 === '1') { echo "selected='selected'"; } ?>>Mark</option>
<option value="2" <?php if ($drpdown1 === '2') { echo "selected='selected'"; } ?>>Extreme</option>
// etc.
you have to know more about the dropdown box because you can not put the value inside the
<select value="<?php echo $drpdown1;?>">
you have to compare the value inside the option directly. example
<select name="drpdown1">
<?php
if($drpdown1 == ""){
?>
<option selected></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
<?php
}else if($drpdown1 == "1"){
?>
<option></option>
<option value="1" selected>Mark</option>
<option value="2">Extreme</option>
<?php
}
?>
</select>

Session not passing dropdown selected element to other php script

First php script -
<?php
session_start();
?>
<html>
<head>
</head>
<body><form method = "post">
<select name="feature" id="feature">
<?php
?>
<option value > Select Feature</option>
<?php
foreach($newFeature as $feat)
{
?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
<?php
}
?>
</select>
</form>
</body>
</html>
<?php
$_SESSION['feature'] = $feature;
?>
second php script -
<?php
session_start();
echo $_SESSION['feature'];
?>
When I run second php script, I get Array as echoed element instead of the element I selected.
What is wrong with the logic here ?
Please guide.
You have to submit the select. It is impossible to set $feature at that moment because the user hasn't yet selected anything.
<form method = "post">
<select name="feature" id="feature">
<option value > Select Feature</option>
<?php foreach($newFeature as $feat) : ?>
<option value="<?php echo $feat;?>" <?= $feat == $_SESSION['feature'] : " selected = 'selected'" : "" ?>><?php echo $feat;?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="send" name="mySubmit" />
</form>
When you hit 'send' you can get the value by using $_POST['feature']; on the same page. If you want to go to another page you have to set the form action property.
session_start();
$_SESSION['feature'] = $_POST['feature'];
After the submit the page will 'reload'. Check if mySubmit is set and set the $_SESSION['feature'](don't forget to start your session at top of the page):
if (isset($_POST['mySubmit'])){
$_SESSION['feature'] = $_POST['feature'];
}

How to send $_POST values from form to image tag

How can I send $_POST values from a form to an image tag? Image is created by another PHP file with post values.
Code example:
<form name="grafiks" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" onsubmit="check()">
<select name="kolsk" onchange="this.form.submit()" >
<option></option>
<option <?php if(isset($_POST['kolsk']) && $_POST['kolsk'] == '1') echo 'selected="selected"' ?> value="1">1</option>
<option <?php if(isset($_POST['kolsk']) && $_POST['kolsk'] == '2') echo 'selected="selected"' ?> value="2">2</option>
<option <?php if(isset($_POST['kolsk']) && $_POST['kolsk'] == '3') echo 'selected="selected"' ?> value="3">3</option>
</select>
</form>
<?php
if(isset($_POST['kolsk'])){
$kolsk = $_POST['kolsk'];
}
echo '<img src = "myplot2.php" ?$kolsk >';
?>
You just need to wrap your server-side code in the <?php ?> tags, just as you do earlier in the file. Something like this:
<?php
if(isset($_POST['kolsk'])){
$kolsk = $_POST['kolsk'];
?>
<img src="myplot2.php?<?php echo $kolsk ?>" />
<?php
}
?>
It's not 100% clear if that src value is a valid URL for your needs. If that's a query string parameter then you'd need a key to go with that value, perhaps something like:
<img src="myplot2.php?kolsk=<?php echo $kolsk ?>" />
Not sure if you're trying to set the image src dynamically or something else, but
<img src = "myplot<?php echo $_POST['kolsk'];?>.jpg" >
should produce myplot1.jpg, myplot2.jpg, myplot3.jpg depending upon the user selection.

PHP drop menu not sticking

I asked this question already but have since over hauled the code following the help I got. I am trying to make my php drop menu sticky but it clears to the top menu item every time after the submit button is pressed. I am not sure where I am going wrong so any help is greatly appreciated. Code as follows:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
if (isset($_POST['Question']))
{
$menuVar = $_POST['fontFamily'];
}
?>
<p id="info-req">How did you find about this site?</p>
<form name="TestMenu" method="post" id="marketing">
<select name="Question">
<option <?php if($menuVar=="----------") echo 'selected="selected"'; ?> value="----------">----------</option>
<option <?php if($menuVar=="WebSearch") echo 'selected="selected"'; ?> value="WebSearch">Web Search</option>
<option <?php if($menuVar=="SocialMedia") echo 'selected="selected"'; ?> value="SocialMedia">Social Media</option>
<option <?php if($menuVar=="Wordofmouth") echo 'selected="selected"'; ?> value="Wordofmouth">Word of mouth</option>
<option <?php if($menuVar=="Other") echo 'selected="selected"'; ?> value="Other">Other</option>
</select>
<input type="submit" />
</form>
</body>
</html>
First of all, there is no $POST['fontFamily'] variable in your form. Why are you trying to use it?
You should use $_POST['Question'] in order to get this value.
So it should be:
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
}
Also you should init $menuVar if there's no $POST in order not to get a Notice: Undefined variable $menuVar. So in the end you code should be:
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
} else {
$menuVar = "----------";
}

Categories