Change value by JavaScript - php

The value of the button should change in 1 or 0, but the echo $_POST["ordina"] give always 1 and I don't understand because the code seems correct.
<script> function order() {
if (document.ordination.ordina.value == "1") {
document.ordination.ordina.value = "0";
} else {
document.ordination.ordina.value = "1";
} }</script>
<?php echo $_POST["ordina"]; ?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" onclick="order();return true;">Ordina</button>
The alert(document.ordination.ordina.value) give always 1.
Some can help me?

Check it now..
<script> function order() {
if (document.ordination.ordina.value == "1") {
alert(document.ordination.ordina.value); // this one shows 1
document.ordination.ordina.value = "0";
} else {
alert(document.ordination.ordina.value); // this one shows 0
document.ordination.ordina.value = "1";
} }</script>
<?php echo $_POST['ordina'];?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="<?php if(isset($_POST['ordina'])){echo $_POST['ordina'];}else{ echo '1';}?>" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>
after you submit this form the <?php echo $_POST['ordina'];?> is 0...
and set to button value as 0 and again submit the value can be changed to 1.
and its consequently changed 0 to 1 to 0 to 1 but you have first time load this page means the alert shows 1 only..

This always return 1, because your button is a "submit" button, so the body is reloaded each time you click on the button.
<script>
function order() {
if (document.ordination.ordina.value == "1") {
document.ordination.ordina.value = "0";
} else {
document.ordination.ordina.value = "1";
}
alert(document.ordination.ordina.value);
}
</script>
<form id="ordination" name="ordination" method="POST" action="">
<button type="button" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>
</form>
I change type "submit" for "button", then it works.

<script> function order() {
if ($("#ordina").val() == "1") {
$("#ordina").val(0);
} else {
$("#ordina").val(1);
} }</script>
<?php echo $_POST["ordina"]; ?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>

Related

PHP actions with multiple Buttons resetting others

here you see my code example:
<?php
session_start();
$arbeit = $_GET["arbeit"];
echo "Angelegte Prüfung: ";
echo $arbeit;
$damen = 0;
$herren = 0;
if (array_key_exists('add_Fem',$_POST)) {
$damen = 1; // BESETZT DAMEN
}
elseif (array_key_exists('sub_Fem',$_POST)) {
$damen = 0; // Reset counter
}
elseif (array_key_exists('add_Mal',$_POST)) {
$herren = 1; // BESETZT HERREN
}
elseif(array_key_exists('sub_Mal',$_POST)) {
$herren = 0; // Reset counter
}
?>
<form method='post'>
<input name='add_Fem' type="submit" value='Damen' class="button">
</form>
<form method='post'>
<input name='sub_Fem' type="submit" value='Reset Damen' class="button">
<h3><em>
<?php
if ($damen == 1) {
echo "DAMEN BESETZT!";
} else {
echo "DAMEN FREI!";
}
?>
</em></h3>
</form>
<form method='post'>
<input name='add_Mal' type="submit" value='Herren' class="button">
</form>
<form method='post'>
<input name='sub_Mal' type="submit" value='Reset Herren' class="button">
<h3><em>
<?php
if ($herren == 1) {
echo "HERREN BESETZT!";
} else {
echo "HERREN FREI!";
}
?>
</em></h3>
</form>
the problem is, when i'm try to set both to 1(male and female) the other variable gets reset to 0. And i dont know why they are affect each other!
Also PHP isset not solves my problem, same situation, that was my first try.
I hope you can give me some advice.
Thank you

checkbox handling php multiple checkbox

Working on a simple php code. When it press on only PH it show hello, and only on chlorine it show yello. When both is pressed it show sello.
<?php
if(isset($_POST['submit'])){
foreach($_POST['verdi'] as $animal){
if(isset($_POST['verdi[]==PH']))
{
echo "hello";
}
}
}
?>
<form name="input" action="" method="POST">
<input type="checkbox" name="verdi[]" value="PH">PH<br>
<input type="checkbox" name="verdi[]" value="Chlorine">Chlorine<br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can do a simple check in PHP:
if( in_array("PH", $_POST["verdi"]) ){
echo "in array!";
}
if(isset($_POST['submit']) && is_array($_POST['verdi'])) {
$checked = array();
foreach($_POST['verdi'] as $animal) {
// you can do extra validation here.
$checked[] = $animal;
}
if(in_array("PH", $checked) && in_array("Chlorine", $checked)) {
echo "sello";
} else {
if(in_array("PH", $checked)) {
echo "hello";
} else if(in_array("Chlorine", $checked)) {
echo "yello";
}
}
}

how to save data on javascript form

I have those 2 function on JS :
<script>
function inc(elem)
{
elem.value++;
}
function dec(elem)
{
if(elem.value>1)
{
elem.value--;
}
}
</script>
And this form in html:
<form method="POST" action= "tameio.php" >
<input type="hidden" value="0" id="counter" name="counter">
<input type = "submit" value = "NextPage" name = "submit" onClick="inc(document.getElementById('counter')); ">
<input type = "submit" value = "PreviousPage" name = "submit" onClick="dec(document.getElementById('counter'));">
</form>
And here is the php :
<?php
if(isset($_POST['submit']))
{
echo $_POST['counter'];
if($_POST['submit'] == 'NextPage'){
...
}
}
?>
So the problem is that after i click NextPage button, 0 goes 1 but it is not stored in the form so it goes 0 again on html . If i reclick it it is going to send 1 again. Should I use ajax instead ?
There is no need to do any of that,
<form method="POST" action= "tameio.php" >
<input type="hidden" value="<?=$current_page?>" id="counter" name="counter">
<input type = "submit" value = "NextPage" name = "submit" onClick="inc(document.getElementById('counter')); ">
<input type = "submit" value = "PreviousPage" name = "submit" onClick="dec(document.getElementById('counter'));">
</form>
And in PHP:
<?php
if(isset($_POST['submit']))
{
if($_POST['submit'] == 'NextPage')
{
$current_page = $_POST['counter'] + 1;
} else {
$current_page = $_POST['counter'] - 1;
}
} else {
$current_page = 1;
}
// Fetch the page data based on the current page
...
?>>
But I would recommend you do this with a GET instead of a post.
Previous Page |
Next Page
And in PHP:
<?php
if(isset($_GET['page']) && $_GET['page'] > 0)
$current_page = (int) $_GET['page'];
else
$current_page = 1;
Try like this-
<script>
function inc(elem)
{
document.getElementById('counter').value = elem.value +1;
}
function dec(elem)
{
if(elem.value>1)
{
document.getElementById('counter').value = elem.value -1;
}
}
</script>

Php Ajax form submit in colorbox

I have a form with some php to validate and insert in the database on submit and the form opens in colorbox.
So far so good. What I'm trying to do is to close colorbox and refresh a div on success.
I guess I need to pass a response to ajax from php if everything OK, close the colorbox with something like setTimeout($.fn.colorbox.close,1000); and refresh the div, but I'm stuck because I'm new in ajax.
I'll appreciate any help here.
Here is my ajax:
jQuery(function(){
jQuery('.cbox-form').colorbox({maxWidth: '75%', onComplete: function(){
cbox_submit();
}});
});
function cbox_submit()
{
jQuery("#pre-process").submit(function(){
jQuery.post(
jQuery(this).attr('action'),
jQuery(this).serialize(),
function(data){
jQuery().colorbox({html: data, onComplete: function(){
cbox_submit();
}});
}
);
return false;
});
}
form php code:
<?php
error_reporting(-1);
include "conf/config.php";
if(isset($_REQUEST['rid'])){$rid=safe($_REQUEST['rid']);}
if(isset($_REQUEST['pid'])){$pid=safe($_REQUEST['pid']);}
$msg = '';
if (!$_SESSION['rest_id']) $_SESSION['rest_id']=$rid; //change to redirect
$session_id=session_id();
if(isset($_REQUEST['submit'])){
if(isset($_POST['opta'])){
$opta=safe($_POST['opta']);
$extraso = implode(',',array_values( array_filter($_POST['opta']) ));
}
if (array_search("", $_POST['opt']) !== false)
{
$msg = "Please select all accessories!";
}else{
$extrasm = implode(',',array_values( array_filter($_POST['opt']) ));
if ($_POST['opt'] && isset($_POST['opta'])) {$extras= $extrasm .",". $extraso;}
if ($_POST['opt'] && !isset($_POST['opta'])) {$extras= $extrasm;}
if (!$_POST['opt'] && isset($_POST['opta'])) {$extras= $extraso;}
$sql['session_id'] = $session_id;
$sql['rest_id'] = $_POST['rid'];
$sql['prod_id'] = $_POST['pid'];
$sql['extras'] = $extras;
$sql['added_date'] = Date("Y-m-d H:i:s");
$newId=insert_sql("cart",$sql);
}
}
?>
<form id="pre-process" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div style="background-color:#FFF; padding:20px;">
<?=$msg;?>
<?php
$name = getSqlField("SELECT name FROM products WHERE resid=".$_SESSION['rest_id']." and id=".$pid."","name");
echo "<div style='color:#fff; background-color:#F00;padding:10px;' align='center'><h2>".$name."</h2></div><div style='background-color:#FFF; padding: 20px 70px 30px 70px; '>Please select accessories.<br><br>";
$getRss = mysql_query("SELECT * FROM optional_groups_product where prodid=".$pid." order by id asc");
while ($rsrw = #mysql_fetch_array($getRss)) {
$goptionals = getSqlField("SELECT goptionals FROM optionals_groups WHERE resid=".$_SESSION['rest_id']." and id=".$rsrw['goptid']."","goptionals");
$goptionals=explode(', ',($goptionals));
echo "<select name='opt[]' id='opt[]' style='width:220px;'>";
echo "<option value='' >Select Options</option>";
foreach($goptionals as $v)
{
$vname = mysql_query("SELECT * FROM optionals where id=".$v." LIMIT 0,1");
while ($rsgb = #mysql_fetch_array($vname)) {
$aa=$rsgb['optional'];
}
echo "<option value=".$v." >".$aa."</option>";
}
echo "</select>(required)<br>";
//}
}
$getRss = mysql_query("SELECT * FROM optional_product where prodid=".$pid."");
?>
<br><br>
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<td bgcolor="#EAFFEC">
<div style="width:440px; ">
<?php
while ($rssp = #mysql_fetch_array($getRss)) {
$optional=getSqlField("SELECT optional FROM optionals WHERE id=".$rssp['optid']."","optional");
$price=getSqlField("SELECT price FROM optionals WHERE id=".$rssp['optid']."","price");
?>
<div style="width:180px;background-color:#EAFFEC; float:left;padding:10px;""><input type="checkbox" name="opta[]" id="opta[]" value="<?=$rssp['optid']?>" /> <i><?=$optional?> [<?=CURRENCY?><?=$price?> ]</i> </div>
<?php } ?>
</div>
</td>
</tr></table>
<input type="hidden" name="rid" value="<?=$rid?>" />
<input type="hidden" name="pid" value="<?=$pid?>"/>
</div><input type="hidden" name="submit" /><input id='submit' class="CSSButton" style="width:120px; float:right;" name='submit' type='submit' value=' Continue ' /><br />
<br /><br />
</div>
</form>
I don't know colobox, but if I understand well what you are trying to do,
I would say your javascript should more look like this
function cbox_submit()
{
jQuery("#pre-process").submit(function(e) {
e.preventDefault(); // prevents the form to reload the page
jQuery.post(
jQuery(this).attr('action')
, jQuery(this).serialize()
, function(data) {
if (data['ok']) { // ok variable received in json
jQuery('#my_colorbox').colorbox.close(); // close the box
}
}
);
return false;
});
}
jQuery(function() {
jQuery('#my_colorbox').colorbox({
maxWidth: '75%'
, onComplete: cbox_submit // Bind the submit event when colorbox is loaded
});
});
You should separate at least your php script that does the post part.
And this php (called with jQuery(this).attr('action')) should return a json ok variable if successfull. Example:
<?php
# ... post part ...
# if success
ob_clean();
header('Content-type: application/json');
echo json_encode(array('ok' => true));
?>

How can i get checkboxes values while transfering in second page?Javascript

i have 5 checkboex. How can I get the value of them if any of them is checked and submited into the second page.
here is my html code.
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1">y
<input type="CHECKBOX" name="CHECKBOX_2" value="2">o
<input type="CHECKBOX" name="CHECKBOX_3" value="3">t
<input type="SUBMIT" value="Submit!">
</form>
here is Javascript code
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if (
theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false)
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
return true;
}
}
</script>
My dear first you need to change the names of your checkboxes to any array like this
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX[]" value="1">y
<input type="CHECKBOX" name="CHECKBOX[]" value="2">o
<input type="CHECKBOX" name="CHECKBOX[]" value="3">t
<input type="SUBMIT" value="Submit!">
</form>
On your test.php just traverse the whole array of CHECKBOX like this
for($_POST['CHECKBOX'] as $key=>$value)
{
if(isset($key))
echo('check box is checked and do some thing with '.$value);
else
echo('check box is not checked');
}
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if(theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false )
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
var val=''; var arr = new Array();
if(theForm.CHECKBOX_1.checked == true){
val += theForm.CHECKBOX_1.value; arr.push(/*push val*/);
}
if(theForm.CHECKBOX_2.checked == true){
val += theForm.CHECKBOX_2.value;
}
if(theForm.CHECKBOX_3.checked == true){
val += theForm.CHECKBOX_3.value;
}
alert(val); //echo out array elements
}
}
</script>
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1"/>y
<input type="CHECKBOX" name="CHECKBOX_2" value="2"/>o
<input type="CHECKBOX" name="CHECKBOX_3" value="3"/>t
<input type="SUBMIT" value="Submit!">
</form>
[UPDATE]::
<?php
for($_POST['CHECKBOX'] as $key=>$value)
{
if(isset($key))
echo "var val[".++$count."] = '".$value."'";
}
?>
<script type="text/javascript">
for(var i=0; i<val.length; i++)
alert(val[i]);
</script>

Categories