How do I conditionally set a form action via Javascript? - javascript

I'm trying to conditionally set the value of a form action attribute depending on whether a checkbox is ticked or not.
I have the following Javascript code:
function validate()
{
var f = document.getElementById("confirmForm");
f.setAttribute('method',"post");
if (document.getElementById('confirmOptionCompare').checked)
{
f.setAttribute('action',"comparison.php");
} else {
{
f.setAttribute('action',"bookConfirm.php");
}
}
}
return true;
}
I'm including the above in my HTML document as follows:
<script type="text/javascript" src="code/scripts.js"></script>
I'm trying to trigger it as follows, but it doesn't navigate to the page I need it to navigate to:
<form id="confirmForm" onsubmit="return validate()">
<p>Click confirm to go to the next step:</p>
<button type="submit" name="confirm">Confirm</button>
</form>
The checkbox that should trigger the if is worded as follows (in a table):
<tr>
<td><?php echo $bookingOutput['name']." ".$bookingOutput['surname'];?></td>
<td><?php echo $bookingOutput['email']; ?></td>
<td><?php echo $bookingOutput['checkIn']." -> ".$bookingOutput['checkOut'];?></td>
<td><?php echo $bookingOutput['daysStaying']; ?></td>
<td><?php echo $bookingOutput['hotel'];?></td>
<td><?php echo "R".$dailyRate ?></td>
<td><?php echo $confirmationObj->ratePerDay; ?></td>
<td><input type="checkbox" name="confirmOptionCompare" id="confirmOptionCompare"/> </td>
</tr>
The checkbox is the final option above.
I'm not sure what I'm doing incorrect here. Any assistance would be greatly appreciated.

Your code should work
In any case here is simpler version
document.getElementById("confirmForm").addEventListener("submit",function() {
this.method="post"
this.action = document.getElementById('confirmOptionCompare').checked ? "comparison.php": "bookConfirm.php";
})
<form id="confirmForm">
<p>Click confirm to go to the next step:</p>
<button type="submit" name="confirm">Confirm</button>
</form>
<input type="checkbox" name="confirmOptionCompare" id="confirmOptionCompare"/>
That said - you can submit and do a redirect simply by setting the header in the PHP
if (isset($_POST["confirmOptionCompare"])) header("location: comparison.php");
die();

Related

I cant run condition IF [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
I have code to filter by dates, I have 2 condition,
First condition: get 'tanggal' from date picker on view
Second condition: variable 'tanggal' isNull
However, the code only runs the second condition, even though the condition variable date is given
This is the code:
View:
.... some code ....
<form method="POST">
<div class="form-group">
<label>Date</label>
<div class="input-group date">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
<form method="get">
<input type="date" name="tanggal">
<input type="submit" value="FILTER">
</form>
.... some code ....
<tbody>
<?php
foreach ((array)$getDepartDetail as $us) { ?>
<tr>
<td><?php echo $us->Name ?></td>
<td><?php echo $us->NIK ?></td>
<td><?php echo $us->gender ?></td>
<td><?php echo $us->PositionDesc ?></td>
<td><?php echo $us->Shift ?></td>
<td><?php echo $us->tgl ?></td>
<td><?php echo $us->Attendance ?></td>
<td><a class="btn btn-info" href="<?php echo site_url('data_detail/detail_datas/vieworc/' . $us->NIK); ?>">Detail</a></td>
</tr>
<?php } ?>
</tbody>
.... some code ....
Model:
.... some code ....
public function get_allDprtDetail($DepartmentID)
{
if(isset($_GET['tanggal'])){
$tgl = $_GET['tanggal'];
$datas = "
SELECT DISTINCT
.... some code ....
FROM
emp0001
INNER JOIN emp0003 ON emp0003.DepartmentID = emp0001.DepartmentID
LEFT JOIN v_dvc0004_test ON emp0003.NIK = v_dvc0004_test.NIK
AND DATE(v_dvc0004_test.Enroll) = '$tgl'
JOIN emp0002 ON emp0002.PositionID = emp0003.PositionID
LEFT JOIN shift ON emp0003.Shift_ID = shift.Shift_ID
WHERE
emp0001.DepartmentID = '$DepartmentID'
AND emp0003.IsActive = 'T'
ORDER BY
v_dvc0004_test.Enroll ASC
";}
else{
$datas = "
SELECT DISTINCT
.... some code ....
}
$query = $this->db->query($datas);
return $query->result()
;}
Firstly you should add a check for the results count ...
<tbody>
<?php if(count($getDepartDetail)> 0){ ?>
<?php foreach ($getDepartDetail as $us) { ?>
<tr>
<td><?php echo $us->Name ?></td>
<td><?php echo $us->NIK ?></td>
<td><?php echo $us->gender ?></td>
<td><?php echo $us->PositionDesc ?></td>
<td><?php echo $us->Shift ?></td>
<td><?php echo $us->tgl ?></td>
<td><?php echo $us->Attendance ?></td>
<td><a class="btn btn-info" href="<?php echo site_url('data_detail/detail_datas/vieworc/' . $us->NIK); ?>">Detail</a></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
Function get_allDprtDetail can return null if $_GET['tanggal'] doesn't set set. You may try to return empty array, like that
public function get_allDprtDetail($DepartmentID)
{
if(isset($_GET['tanggal'])){
some code
}
return [];
}

hide/show columns $row depending checkbox or radio

I need to create a checkbox that hide the rows that contain MULTIPLE in CARS value from a CRUD.
This is the part where I need to implement the checkbox:
<body>
<?php while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ; ?>
<form name="form">
Hiden Multiple: <input type="checkbox" id="ck"
***onclick="hide if $row['Cars']= Multiple, if not show :D "***/>
</form>
<table id="tabla" border="1">
<tr>
<td><?php echo $row['ID']; ?></td>
</tr>
<tr>
<td><?php echo $row['Name']; ?></td>
</tr>
<tr>
<td><?php echo $row['Years']; ?></td>
</tr>
<tr>
<td><?php echo $row['Cars']; ?></td>
</tr>
</table>
</body>
I can point you in the direction, but you need to code it yourself.
PHP / HTML: you need one form, so place your outside your while-loop
PHP / HTML: add an css class to the table when condition "$row['Cars'] == 'Multiple'" is true eq:
Javascript: hide / show elements with class "has_multiple_cars" when checking/unchecking the checkbox.
Yes, only found this java and work but with de number of the line:
<script type="text/javascript">
function ocultarFila(numFila) {
var form = document.form;
fila = document.getElementById('tabla').getElementsByTagName('tr')[numFila];
if(form.ck.checked == true) {
fila.style.display = 'none';
} else {
fila.style.display = '';
}
}
</script>
</head>
<body>
<?php while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ;
?>
<form name="form">
Hiden Multiple: <input type="checkbox" id="ck" onclick="ocultarFila(1)"/>
</form>
Can make a condition for me? I'm starting to program and I do not find the right sentence, thanks!

disable checkbox/textbox dynamically after insert

I've been trying to disable checkbox(which I fetched using while loop in php) after form submit. Any help would be appreciated.
<td><?php echo $num['roomno']; ?></td>
<td><?php echo $num['regno']; ?></td>
<td><?php echo $num['pname']; ?></td>
<td><input type="checkbox" name="diet" id="diet" value="<? echo $num['regno'];?>"
<?php if(($regno!="") && ($thiss == $regno)) { echo 'disabled'; } ?> />
</td>
This is the php section to get the value from checked checkbox. I dont know if I'm doing the right way.
<?
$regno = isset($_REQUEST['diet'])?$_REQUEST['diet']:"";
if(isset($regno)) {
$selec = "select regno from roomlist where regno='".$regno."'";
$sql = mysqli_query($con,$selec);
if(!$sql) {
echo "Error : ".mysqli_error($sql);
exit();
}
$num = mysqli_fetch_array($sql,MYSQLI_ASSOC);
$thiss = $num['regno'];
}

Click function showing div elements when not clicked out

I have an admin panel that I am creating. I have a left panel section and then the right side which shows the div when the panel button is clicked. I created a fiddle to show what it looks like and to help me explain this...
https://jsfiddle.net/jq8c51c9/
In the Fiddle it works just like it should, but I took out all of my php. The problem is around the Announcements div, it shows the div for the League Dues under it. Also once you click on announcements and then click on another panel button, if I click on Announcements again the only thing that will show up is this..
Announcements Current Announcements
League Dues
Again this is NOT doing this in the Fiddle.
Here is the full code for the area that the issue resides in. I have been stuck on this forever and cannot figure out why I am having difficulties with only these two divs.
Does anyone see what it is that I am doing wrong?
Announcements
try {
//Prepare
$con = mysqli_connect("localhost", "", "", "");
if ($user_stmt = $con->prepare("SELECT `id` FROM users")) {
$user_stmt->execute();
$user_stmt->bind_result($user_id);
if (!$user_stmt) {
throw new Exception($con->error);
}
$user_stmt->store_result();
$user_result = array();
//while ($user_row = $user_stmt->fetch()) {
?>
<div class="announcement_success"></div>
<p>Add New Announcement</p>
<form action="" method="POST" id="insert_announcements">
<input type="hidden" value="<?php echo $userid; ?>" id="approved_id" name="user_id" />
<textarea rows="4" cols="50" id="announcement_message" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
<label for="contactButton">
<button type="button" class="contactButton" id="submit_announcement">Add Announcement</button>
</label>
</form>
<?php
if ($announcements_stmt = $con->prepare("SELECT * FROM announcements")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
$announcements_stmt->store_result();
$announcements_result = array();
?>
Current Announcements
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
<?php
while ($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
</table>
<?php
}
}
}
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}
?>
</div>
<div id='dues'>League Dues</div>
</div>
You have an error when building the announcement-table.
The closing </table> tag is inside the while loop, so the html will be screwed up, making everything after the closed table disappear.
So change the while loop to:
....
<?php
while ($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
<?php
}
?>
</table>
<?php
....
Anyway, I recommend to build your html first in a variable and echo it out later alltogether. That makes cleaner code and reduces risk of inconsitency of html tags. When using with HEREDOC you even don't have to bother about quotes.
In your case that could for example look like that:
<?php
$table_head = <<<EOT
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
EOT;
$table_content = "";
while ($row = $stmt->fetch()) {
$table_content.= <<<EOT
<tr>
<td>$announcements_id</td>
<td>$announcements_username</td>
<td>$announcements_messages</td>
<td>$announcements_date</td>
</tr>
EOT;
}
$announcement_table = "<table>".$table_head.$table_content."</table>";
echo $announcement_table;

Dynamically adding and deleting table rows - Yii Framework

I am trying to create form by dynamically adding rows and deleting it when user clicks on delete button using php code,
below is my code to render first row while opening the form ,
<div class="selector-details" style="display:none">
<div class='newfield'>
<div id='container'>
<table id="tid">
<tr>
<td><?php echo CHtml::dropDownList('field_list','',$field_name); ?></td>
<td><?php echo CHtml::dropDownList('field_list','',$operator); ?></td>
<td><?php echo CHtml::textField('querybox'); ?></td>
<td> <?php echo CHtml::imageButton(Yii::app()->request->baseUrl.'/images/Trash.jpg',array('class'=>'trash-action')); ?></td>
<?php echo "<br>"; ?>
<td> <?php echo CHtml::dropDownList('condition_check','',$condition_check);?></td>
</tr>
</table>
</div>
</div>
<?php
echo CHtml::button('Add',array('class'=>'addfield-button','background-style'=>'none'));
how i should make call the above code to add rows and delete particular row when user clicks on the row delete button ? I am new to yii please provide any idea to go further.
$script = 'alert("hello")';
Yii::app()->getClientScript()->registerScript('#test', $script,CClientScript::POS_HEAD );
echo CHtml::button('Add',array('class'=>'addfield-button','background-style'=>'none','onclick'=>$script));
if you real want to use Yii style, you can open framework code and there is a good code that you want, search : CButtonColumn.php

Categories

Resources