To have external scope for variables in php - javascript

I am simply trying to store $username in a file that will be included in all the pages after a user logins.Thus, this $username will change dynamically according to the user that has logged in.
includeusername.php
<?php
$username=$_POST['username'];
echo $username;
?>
The variable does get initialized in includeusername.php ,as echo gives the value.On including this file in the main page which will display "Welcome $username!" i write this code
Welcome <?php echo $username; ?>!
The error shown is Undefined index: username.
How to have access to data from other page?And transfer it to the next page too!
I've used the following but it didn't work:
sessionStorage()
$_SESSION['username']
include/require
Any other idea to have persistent data across the web pages?

<?php
session_start();
$_SESSION["username"]=$_REQUEST['username'];
?>
In your includeusername.php use
Welcome <?php echo $_SESSION["username"]; ?>!

start the see SESSION START
<?php
session_start();
$username=$_POST['username'];
echo $username;
echo "Assigning session value to: " . $username;
$_SESSION['username'] = $username;
?>

Related

I want generate an alert dialog if login fail in PHP,JS but finding issue

if($row['psw']===$passwordphp)
{
echo '<script language="javascript">';
echo 'alert("LOGIN SUCCESSFULL")';
echo '</script>';
header("Location: registration.php");
}
else{
echo '<script language="javascript">';
echo 'alert("CHECK EMAIL OR PASSWORD")';
echo '</script>';
header("Location: login.php");
}
Here is the code. If I don't add header then JavaScript runs but when I add header Javascript stop running only executes the header function. Why is so? How can make both of them work properly?
A Location header tells the browser to drop everything and do something else. You can't do a Location redirect and output HTML.
That said, this code should throw a "Headers already sent" error message, as you can't do a header call after echoing anything to the browser.
As mentioned in the other answer, you can't send a header redirect and also send HTML.
Instead of the header redirect, you can use a Javascript redirect after the alert.
if($row['psw']===$passwordphp) {
echo '<script language="javascript">';
echo 'alert("LOGIN SUCCESSFULL");';
echo 'window.location = "registration.php";';
echo '</script>';
} else {
echo '<script language="javascript">';
echo 'alert("CHECK EMAIL OR PASSWORD");';
echo 'window.location = "login.php"';
echo '</script>';
}

Run script and php after form submission

I have an html form like this:
<form method="get" action="save.php">
<input type="email" id="email" name="email"/>
<input type="submit" name="submit" value="Submit" />
</form>
and in save.php i have something like this:
<?php
session_start();
$email = $_REQUEST['email'];
$email_content = "Thank you for your subscription";
mail($email,"Thank you",$email_content);
header("Location:thankyou.php");
?>
Now in save.php file i need to send this e-mail but also to echo a script that runs a js function. For example
<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "</script>";
?>
Now, if i place the echoing of the script before mail(), then i don't go to thankyou.php, mail() is not executed, i don't go to thankyou.php but script function works. If i place echoing of script after mail, then mail is sent, i go to thankyou.php but script function is not executed at all.
Any ideas to make both happen?
Thank you in advance
It's becouse echo command send content to browser, and header redirect will never works.
You could try to use comething like that:
<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "document.location.href='thankyou.php';";
echo "</script>";
?>
It means, move redirect command from php code to javascript.
Possibly some error in mySubscribe(subscriberEmail); function. Thats is why if you put script before email and header("Location:thankyou.php"); it not sending mail and redirects you. And also if you put script before, $email variable is not set yet

Filter WordPress posts by custom taxonomy term with AJAX

Please advise....
I create filter taxonomy using this tutorial: https://www.bobz.co/blog/demo-filter-wordpress-posts-custom-taxonomy-term-ajax/
It is echoing all my codes but when I click to certain tag it stop showing me images only.
I am using ACF Gallery code for images in template.
<?php
$images = get_field('gallery');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I check ajax filter post file and try to edit code but still fails to do so can you please help me out with it.
I want gallery to be display with certain posts when I click on "TAGS"
$output = '<h2>'. get_the_title() . '</h2>';
$output .= get_the_excerpt();
$result = 'success';
endwhile; else:
$output = '<h2>No posts found</h2>';
$result = 'fail';
endif;
There is not a way we can say whats happening unless seeing it.
Additionally, if you want get the ACF fields from the current page then you must pass the post id. So, it might help you by guessing but not debugging your problem.
$images = get_field('gallery', $post_id);
$post_id should be the id of that particular post you want to show.
Hope this works as guessing your problem for now.
If you give us more details we can see and inspect the problem more easily.
Thanks

How to refresh the page in php after displaying alert

I am updating the records using php/mysql. and after all is done .
I run this code for user confirmation about the activity.
echo '<script language="javascript">';
echo 'alert("We Have credited your account")';
echo '</script>';
How Do i refresh the page so that once user clicks ok, it will refresh all the details again and should display users with new values from the db.
If I use this right after the alert.
header('Location: '.$_SERVER['REQUEST_URI']);
It does not displays the alert message and simply refreshes the page.
Alerts are weird but you can do something like the below as your echo. Alert returns an undefined.
echo "<script language='javascript'>";
echo "if(!alert('We Have credited your account')){
window.location.reload();
}";
echo "</script>";
UPDATE
Right after posting this I realized that the alert will block any other js from running. You can literally just do
echo "<script language='javascript'>";
echo 'alert("We Have credited your account");';
echo 'window.location.reload();';
echo "</script>";
Edit to handle page refresh (submit by OP):
echo "<script language='javascript'>";
echo 'alert("We Have credited your account");';
echo 'window.location.reload();';
echo "</script>";
} else {
echo '<script language="javascript">';
echo 'alert("Please Paste Exact URL Here")';
echo '</script>';
}
window.location.reload and header('Location: '.$_SERVER['REQUEST_URI']) will keep you in a loop "basically" not static "locations", if you die in game you get reloaded to a point or location, you can't replace where you spawn until you got past that point (^.^).
Use window.location.replace("your_page.php") or window.location.href = "your_page.php"; because reload and header is more used for auto redirection to different locations while replace and href is more used to "escape" your current page and load it from scratch.
So do something like this if your query passes:
if ($conn->query($sql) === TRUE) {
echo "<script language='javascript'>";
echo 'alert("Your alert msg");';
echo 'window.location.replace("your_page.php");';
echo "</script>";
}
else{
echo "FAIL";
}
It will prevent data insertion or strings from going in a loop over and over again. I had a pc's USB ports that malfunctioned once and it kept on inserting and alerting in on of my databases it was an EPIC disaster took a while cleaning it up.

php in <script> tags executing without call why?

The php in method signout is executing without call . At first the username is echoed fine , but second echo below signout method shows that its null. It should be called when the singout link is clicked . Why is this happening ?
<?php echo $_SESSION['username']; ?> // this echo show real value
<script>
function singout()
{
<?php $_SESSION['username']='null'; ?>
window.alert("<?php echo $_SESSION['username'];?>");
}
<?php echo $_SESSION['username']; ?> // but this echo show null value
</script>
<a onclick="singout();" href="login.html" style="float: right">singout</a>
Your PHP is being called without the JS Function being called because on browser load each PHP line is procedurally called. Just because your PHP line is within a JS function doesn't mean it isn't called on browser load

Categories

Resources