pass parameter with url ajax to controller - javascript

I have an url in ajax like this:
<html>
<header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</header>
<body>
<?php
echo("<pre>");
echo "Hello: <span style='color:red'>".$user_data['user'];
echo("</pre>");
echo "<button id='play' id_user = '".$user_data['id']."' user = '".$user_data['user']."'>Chơi game</button>";
?>
<script>
$("#play").click(function(){
var user_id = $(this).attr('id_user');
var user = $(this).attr('user');
window.location= "<?php echo site_url('wellcome?user_id='.$user_data['id'].'&user='.$user_data['user']); ?>";
})
</script>
</body>
</html>
When i redirect to welcome Controller with 2 parameters then it shows a 404 error. How should i do pass that parameter to controller welcome? Thanks all!

Things to check for the redirect (not ajax) you are trying to do:
use window.location.href
are you certain, there's something at wellcome?... (welcome typically has a single letter l ?
check in HTML source view, what get's generated behind the equal sign. Does it look like a reasonable URL path?
obviously, you attempt a relative path, see further advice here how to do that.
Arguably better, change to an absolute path, using PHP $_SERVER Variables to glue it together, or at least start with a leading // (path and protocol-relative to your page)

Related

PHP & HTML: how to set url to home page

When I go to the home page of my website, it does not have a default id in url.
How can I set a default id to my home page's url.
I want this : www.afgclick.com&id = 2
Just add a check to see if and id is set, and if not, redirect to the same url WITH that id.
In PHP: (thanks #Albzi)
if (!$_GET['id']) $_GET['id'] = 1;
If you need that parameter in the URL but you dont own the link that gets you to that page then try using something like:
HTML version
Create an index.html file containing the following code in the head section:
<meta http-equiv="refresh" content="0; url=www.afgclick.com&id=2" />
Javascript version
<script>
setTimeout(function(){location.href="http://www.afgclick.com&id=2"} , 3000);
PHP version
function redirect($url, $statusCode = 303){
header('Location: ' . $url, true, $statusCode);
die();
}
Good luck!

$.post() Get url of php in html

I am making an html script that will use jQuery.post() method in order to use a php script. The post() method needs an url of the php code but my php code is part of the html code
What Url should i use?
<html>
<body>
<button onclick="test()">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
var test = function() {
$('--I DONT KNOW--',{testvar:"hello"});
}
</script>
<?php
$testvar = $_POST['testvar'];
echo $testvar;
?>
</body>
</html>
An empty URL is a relative URL that resolves as the URL of the current page.
$.post("", {testvar:"hello"});
(Keep in mind that since your PHP does nothing with the data except output it, and your JS does nothing with the response, this will have no visible effect outside of the Network tab of your browser's developer tools).

Passing a PHP variable into Javascript returns NULL

I know there are a lot of questions covering something similar, but I've tried the answers without any luck.
Snippet of PHP:
$usernum_query = "SELECT numPersonID FROM tbllogins WHERE txtUserName='$currentuser'";
if(!$usernumber = $db1->query($usernum_query)){
die('There was an error running the usernumber query [' . $db1->error . ']');
}
while($row = $usernumber -> fetch_assoc()) {
$userIDnum = $row['numPersonID'];
$userIDnum = utf8_encode($userIDnum);
}
Snippet of Javascript:
$(function(){
$("#0").click(function(){
var userIDnum = <?php echo json_encode($userIDnum); ?>;
alert(userIDnum);
The most common answer I've come across says to UTF_encode my variable, which I think I've done correctly. I've also tried:
var userIDnum = <?php echo $userIDnum; ?>;
Which doesn't work.
In my HTML outside of the script,
<?php echo json_encode($userIDnum); ?>
return "90" (with the quotes)
<?php echo $userIDnum; ?>
returns 90 (without the quotes).
Within the script, I get null and no alert box, respectively.
Any ideas as to why the variable isn't passed into the script? Thanks!
edit: tried with quotes and got the same result
[Taken from comments as requested]
If I can make a recommendation, put your value in a data-attribute in the HTML (e.g. <body data-user-id="<?php echo $userId ?>">. This keeps you from mixing code languages together which is hard to read, and would allow your external script to run correctly without making it a PHP page.
As far as IE9, I'll take a quick look. You might want to see how jQuery manages the data attributes. You should, at the least, have access to
domObject.getAttribute('data-user-id').
Yep, just did a quick lookup, and even IE10 doesn't support the dataset feature. So, you'll need to use getAttribute for IE <= 10.
A solution based on a suggestion by calamari:
In the HTML:
<!DOCTYPE html>
<!--[if lt IE 11 ]> <html class="ie10orless"> <![endif]-->
<head>
... more code here ...
And in the script:
var oldIE;
if ($('html').is('.ie10orless')) {
oldIE = true;
}
if (oldIE) {
var x = document.getElementsByTagName("div")[0].getAttribute("data-number");
alert("in IE");
alert(x);
} else {
var userinfo = document.querySelector('#user');
alert("NOT in IE");
alert(userinfo.dataset.number);
}
Hope this helps someone else. Thanks everyone for the other suggestions as well.
There are couple of things to check. Lets say this is your script:
<?php $htmlString= 'testing'; //Lets say this is a snippet of your php
?>
<html>
<body>
<script type="text/javascript"><!-- And this is the snippet of your JS -->
// notice the quotes around the ?php tag
var htmlString="<?php echo $htmlString; ?>"; //Make sure there's double
//quotes around your php
alert(htmlString);
</script>
</body>
</html>
Make sure you have PHP and JS in a file called .php. If its an external .js file, php inside it will not work (you can rename the JS file to a .php extension if you have it set up that way)
Also make sure you have the html, php and js setup the way above. There has to be double quotes around php, for example:
var userIDnum = <?php echo json_encode($userIDnum); ?>;
change this to below:
var userIDnum = "<?php echo json_encode($userIDnum); ?>";

Facebook page tab redirect to fan page after form submit [duplicate]

I have a simple form which is inside IFRAME. When user click on SUBMIT, it redirects to a specific page on my server. The function I use for the redirect is
header ('Location: mypage2.html');
exit ();
But I want the new page to open in _top location, not inside the same IFRAME that I use. How can I tell the browser to open the new page in _top not inside the IFRAME?
Thanks in advance.
You are not able to achieve the desired effect in PHP. This is something you'd have to do from JavaScript or add target attribute to <form>:
<form ... target="_top">
You can use javascript to access the parent. You could echo out javascript in your PHP.. so your parent page has this:
function changeURL( url ) {
document.location = url;
}
and in your php script, you echo
<script>
parent.changeURL('mypage2.html' );
</script>
The reason you can't call parent.document.location is because it's read only - you have to have a function available on the parent to do it.
A simple way directly in PHP to redirect the parent page rather than the iframe:
echo "<script>top.window.location = '/mynewpage.php'</script>";
die;
The die; isn't necessarily necessary, but it is good "just in case" to prevent the script from continuing any further, for example, if javascript is disabled in the user's browser.
we can use javascript like this :
target.window.location='locationpage.html';
top.window.location='mypage2.html';
The best and simplest thing to do is to use the form target element
<form action="..." target="_top">
<form action="..." target="_parent">
either of the target parameters works fine
You can either link to the page using Break Out or use code
<?php header("Location: pagename.php?Break=Y"); ?>
You then use the following code in the header of the page with
if(isset($_GET['Break'])) //
{
$BreakFrame = true;
$BreakToPage = "pagename.php";
}
<script language="JavaScript" type="text/javascript">
function changeURL( url ) {
document.location = url;
}
</script>
<?php if($BreakFrame) { ?>
<script language="JavaScript" type="text/javascript">
parent.changeURL('<?=$BreakToPage?>' );
</script>
<? }?>
In the page that you want to load in _top, add the follow code in the header
<script type="text/javascript">
if (top != self) top.location.href = location.href;
</script>
For CakePHP 4, to redirect your parent page just add option 'target'=>'_top' in your iframe's link:
Example:
<?= $this->Html->link(__('Redirect Parent'), ['controller' => 'Users', 'action' => 'view'], ['target'=>'_top']) ?>
All the best!
You can add multiple headers to your redirect.
I use a little function that I created with this in mind.
public function redirect($url){
header('Window-target: _top');
header('Location:' . $url, true, 303);
exit();
}

Echo php var before the </head> (closing head) tag in order to retrieve it with java script

I have a php variable called $myVar = "6542";
Where can I echo it before the </head> tag of my site (and not trigger any html issues) in order to be able to pick the value up with javascript?
Ty
Throw it above your tag somewhere. Simple.
<script type="text/javascript>var myvar = '<?=htmlspecialchars($myVar);?>';</script>
It can be put anywhere on the page, not necessarily needing to be above
Just about anywhere (the HEAD contents is the best place), provided that you wrap it up with a <script> tag:
<script>
var myVar = "6542";
</script>

Categories