deleting an alert in an "if" statement - javascript

I have an if statement that currently has an alert pop up when the next button on the page is clicked. Im not sure what i have to remove to just have the alert go away. I still need the button to take you to the next page. No matter what I adjust, the end result is the button no longer works. Here is the code:
$('.dab_button').click(function(){
if($('[name=select_value1]').val()=='' ||
$('[name=select_value2]').val()=='' ||
$('[name=select_value3]').val()==''){
alert('Please select your gift choices for each or the three levels.');
} else{
<? if($members_info['individual']=='no'):?>
window.location.href = "/dab/profile";
<? else:?>
window.location.href = "/hq/edit_page";
<? endif;?>
}
});

Just comment it out?
$('.dab_button').click(function(){
if($('[name=select_value1]').val()==""||$('[name=select_value2]').val()==""||$('[name=select_value3]').val()==""){
; // do nothing
// alert('Please select your gift choices for each or the three levels.');
} else{
<? if($members_info['individual']=='no'):?>
window.location.href = "/dab/profile";
<? else:?>
window.location.href = "/hq/edit_page";
<? endif;?>
}
});
But I stronly recommend to give some feedback to the user when the button does nothing (does not navigate away), because they have not filled out all inputs. If you wanted to navigate in any case, just remove that if-statement (note that you also have serverside processing code, which has nothing to do with the javascript):
$('.dab_button').click(function(){
<? if($members_info['individual']=='no'):?>
window.location.href = "/dab/profile";
<? else:?>
window.location.href = "/hq/edit_page";
<? endif;?>
});

Related

Is the function binded to the button always executed or did I make a mistake?

So I have this PHP page, with at the start some unimportant stuff and a session opening :
<?php
session_start();
?>
I want the disconnect button to appear only if the user is already connected, which means that $_SESSION['Pseudo'] is set. If this is the case, I create a button, and bind to that button a function to destroy the session. The problem is, each time I go on this page, the session is destroy, even though it's in a condition.
Here's my code :
<?php
if (isset($_SESSION["pseudo"])) {
echo '<p><button type="button" id="deco">Disconnect</button></p>';
}
?>
<script>
let btn = document.getElementById('deco');
if (btn) {
btn.addEventListener('click', updateBtn);
function updateBtn() {
<?php
session_destroy();
?>
window.location.href = "/"
}
}
</script>
Any clue ?
You cannot combine PHP within Javascript like that.
When you're doing , it's not a part of the Javascript. It's PHP code and is executed as part of the PHP code in the file.
This means that it has nothing to do with the javascript aspect. your updateBtn function is effectively, when the page source code loads:
function updateBtn() { window.location.href = "/" }
What you really want to do is make it so that when someone clicks the button, it takes them to a logout page or a page with a logout query param.
eg.
<?php
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
session_destroy();
echo 'You have successfully logged out!';
}
if (isset($_SESSION["pseudo"])) {
echo '<p><button type="button" id="deco">Disconnect</button></p>';
}
?>
<script>
let btn = document.getElementById('deco');
if (btn) {
btn.addEventListener('click', updateBtn);
function updateBtn() {
window.location.href = "/?action=logout"
}
}
</script>

Javascript in php refresh page after dialog

Hello I have the below php code:
if (isset($_POST['prano'])) {
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('aplikacioni');
$mbajtesi = $_SESSION['mbajtes'];
for ($i=1; $i<=$mbajtesi; $i++) {
if (empty($notat[$i])) {
$emp = "emp";
}
}
if ($emp == "emp") {
echo ("<script language = 'javascript'> window.alert('Something!!!');window.location.href='Snotat.php';</script>");
exit();
}
}
Now, this code means: When I press the button named 'prano' then displayed a table with input values.The user must fulfill all values..If not then he gets a message...
So I don't want after the dialog message to disappear the table [remember the table appear when press button 'prano']...just I want to appear a dialog message without disappear the table..I've tried location.reload() but not work..any idea?
Your code is OK. Can you check your $_SESSION['mbajtes'] & confirm that, session_start(); line is exist in top of your page?
The problem is that you don't need to reload the page. You should use javascript/jQuery to insert an element to the body (for example) with that message.
Example:
<?php if ($emp == "emp") { ?>
<script>
jQuery(document).ready(function($){
$('body').prepend('<div>Your message Here</div>);
});
</script>
<?php } ?>
Remember to add jQuery to your header or footer.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

Alert box in PHP - Making me crazy

I know this question is very common, I have search alot, and after alot of hard work, I am asking here.
I simply want to show an alert box and redirect it to a page.
The code below is redirecting me, but not showing me the alert box.
Please, let me know my mistake.
<?php
require_once('connection.php');
if(isset($_POST['person'])){ $name = $_POST['person']; }
if(isset($_POST['pwd'])){ $password = $_POST['pwd']; }
if(isset($_POST['position'])){ $pos = $_POST['position']; }
if(empty($name) or empty($password))
{
echo "<SCRIPT type='text/javascript'>
alert('Places should not be empty! Press Ok and try again!');
window.location.replace(\"addmember.php\");
</SCRIPT>";
}
else
{
//$query=mysqli_query($con,"insert into members (username, password,position) values ('$name','$password','$pos')");
echo "<SCRIPT type='text/javascript'>
alert('Added!');
window.location.replace(\"addmember.php\");
</SCRIPT>";
}
?>
I personally make this function to show an alert box and redirect it to a page.
function do_alert($msg,$link=NULL)
{
echo '<script type="text/javascript">
alert("' . $msg . '");
</script>';
if(!empty($link)) {
echo '<script type="text/javascript">
window.location="'.$link.'";
</script>';
}
}
Use this function both alert and redirect to a page use this
do_alert("Message","URL");
do_alert("Here this is your alert","addmember.php");
do_alert("Only your alert");
If the function 2nd parameter URL is empty then it'll only do alert.
try to change
window.location.replace(\"addmember.php\");
to
window.location='addmember.php';

How to load information to another page when button is clicked

When I click a button, I'm trying to get the name of the button to display on the next page. So far, I've managed to display the button name in an alert box but not sure how to get it to appear on another page.
<script>
$(document).ready(function(){
$("#test").click(function(){
alert("Text: " + $("#test").text());
});
});
</script>
The code below gets modules that the logged in user is registered to and displays two buttons. I'm trying to display the button label(subject names) on another page rather than in the alert box. So when I click on a button, the label name appears on the next page.
$sql = DB::getInstance()->get('modules', array('username','=' ,$user->data()->username));
if(!$sql->count()){
echo 'No data';
}else {
foreach ($sql->results() as $sql){ ?>
<p> <?php echo $sql->name; ?></p>
If you are trying to navigate to another page on click, you can just modify your jquery event handler to this and pull it out of the query params on the next page.
$(document).ready(function(){
$("#test").click(function(){
window.location = "your_next_page.php?name=" + $("#test").text();
});
});
on your other page.
<?php
echo $_REQUEST['name'];
?>
Save the $sql->name to session data and use this another page
$sql = DB::getInstance()->get('modules', array('username','=' ,$user->data()->username));
if(!$sql->count()){
echo 'No data';
}else {
foreach ($sql->results() as $sql){
$_SESSION['name'] = $sql->name;?>
<p> <?php echo $sql->name; ?></p>
another_page.php
session_start();
echo $_SESSION['name'];

How can I pass the name of the field in form_error ()

It's part of my view. Me need transmit input name in on which i click.
Below is a script that will get the name input after click
<div class="form_hint"><?=form_error('this get value from javascript after click')?></div>
<?php echo form_open("onenews/" . $onenews['id'] . "#signup", $form_reg['main_form']); ?>
<?php echo form_input($form_reg['login'], $this->form_validation->set_value('login')) ?>
<?php echo form_input($form_reg['email'], $this->form_validation->set_value('email')) ?>
<?php echo form_input($form_reg['password']) ?>
<?php echo form_input($form_reg['conf_password']) ?>
<?= MY_Controller:: create_captcha(); ?>
<?php echo form_input($form_reg['captcha']) ?>
<?php echo form_input($form_reg['submit']) ?>
<?php echo form_close(); ?>
</div>
</div>
jq
<script type="text/javascript">
$(function(){
var curInput = '';
$('#form_reg').find('input').on('click', function(){
curInput = $(this).attr("name");
});
})
</script>
Or i must use ajax?
Thanks!
Your question is not clear at all, but I assume you want to dynamically change the content of the form_hint div. You can't do that with PHP. Once PHP renders the page, it shuts down and does nothing more. So the only way to make PHP show that message is after the form submit, but then you lose your click data. There is a way to save the clicked element in a session for example, but that would be a really bad solution.
So the best solution would probably be to list all the hints in a JavaScript variable, and call the appropriate one upon the click event, and fill the form_hint div with it.
$('.form_hint').text(yourAppropriateMessageHere);
An example with a hidden div for the login input field with exactly how you described would be:
<div class="form_text_login"><?=form_error('login')?></div>
JS
var loginMessage = $('.form_text_login').text();
// list others here
// then make a tree with switch/case or if/else
if (curInput == 'login') {
$('.form_hint').text(loginMessage);
}
else if (curInput == 'email') {
// do the same with the email message, etc.
}

Categories