I'm using the following script to use a popup window to load a $_GET address:
while ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
if($_SESSION['scontact_id'] == 'scontact_id' && $row1['shared_id'] != '' && $row1['shared_id'] != '0'){
echo "<td> <center><button onclick='myFunction()'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center>
<script>
function myFunction(shared_id) {
window.open('". $mywebsite . "vcontact_id=" . $row1['shared_id'] ."' +shared_id, '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script> </td>";
However, it only loads the first row in the loop correctly. After that, it displays the same $_GET address, regardless of where it is in the while loop. It does the same thing if I convert the row to a php variable and pass the PHP variable, to the script, as a JavaScript variable. It works fine, without the popup, like this:
echo "<td>"."<a href='" . $mywebsite . "vcontact_id=" . $row1['shared_id'] . "' target='_blank', '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400'><center><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></center></a>" . "</td>";
I'm not very familiar with JavaScript. Any help would be greatly appreciated.
Chage this part:
echo "<td> <center><button onclick='myFunction()'>
for this:
echo "<td> <center><button onclick='myFunction(\"" . $row1['shared_id'] . "\")'>
EDIT: Or make it one line instead of using a function:
echo "<td> <center><button onclick='window.open(\"". $mywebsite . "vcontact_id=" . $row1['shared_id'] ."\", \"_blank\", \"toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400\");'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center></td>";
myFunction is being overwritten each time in the loop. You could always rewrite the code with an increment counter to change the function name. It's typically better to not mix javascript in php in my opinion. You could always just pass these in as additional parameters into your function where you are outputting php / js.
You are continuously redefining the function: myFunction(). you should output the script tag once, outside the loop, and change the onclick attribute on your button to ...<button onclick='myFunction(" . $row1['shared_id'] . ")'>..., if I am reading your problem correctly
Please try below code:
<?php
while ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
if($_SESSION['scontact_id'] == 'scontact_id' && $row1['shared_id'] != '' && $row1['shared_id'] != '0'){
echo "<td> <center><button onclick='myFunction(".$row1['shared_id'].")'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center></td>";
}
}
?>
<script>
var mywebsite = "<?php echo $mywebsite ?>";
function myFunction(shared_id) {
window.open(mywebsite+"vcontact_id="+shared_id, '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
I believe you simply need to move your script outside the if loop, yet maintain it in the while loop and you need to add your (shared_id) that is required by the function, to the click event.
<?php
while ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
if($_SESSION['scontact_id'] == 'scontact_id' && $row1['shared_id'] != '' && $row1['shared_id'] != '0'){
echo "<td> <center><button onclick='myFunction(".$row1['shared_id'].")'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center></td>";
}
echo "
<script>
function myFunction(shared_id) {
window.open('". $mywebsite . "vcontact_id=" . $row1['shared_id'] ."' +shared_id, '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
";
}
If you care at all about page optimization, it would be far wiser instead of looping the script in the while statement, to set a data variable and a class on the button and use an onclick event to determine the values so you don't have 20 instances of the same script with different values on the page.
For example:
Inside your button:
class='clicked' data-id='".$row1['shared_id']."'
Then use jquery for the script
<script>
$( ".clicked" ).click(function() {
var data = $(this).attr('data-id');
window.open('". $mywebsite . "vcontact_id='+data', '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
This looks for a click event on the class "clicked" which we assigned to the button. When button is clicked, we get the value of "data-id" and set it as a variable "data". We then call that variable instead of echoing a php variable. This allows us to take the script out of the loop and not write it to the page multiple times.
Related
I'm trying to manipulate an amount based on the name of class that changes with jQuery. My amount comes from here:
<script>
var inboundAmount = <?php echo json_encode($inbound); ?>;
</script>
Let's just say that amount is 10.
I output with a bit of php and script like this:
echo "$<script>document.write(inboundAmount);</script>";
The class changes when a user clicks on it. Sample classes are as follows:
half first3547
pending first3547
paid first3547
What I've been trying to do is make it so that if the class contains the word 'half', then it would do some math:
document.write(inboundAmount/2);
Producing the number 5.
I've tried this and some variations with no luck:
if ($("#mycontent").find("a.half").length > 0) {document.write(inboundAmount/2);}
'mycontent' is the id for the table that a href exists in.
I have also tried the following:
if ( $( this ).hasClass( 'half' ) ) { {document.write(inboundAmount/2);}
still to no avail.
A full example is this:
<td class='grav-results-td'>
<?php if (!empty($check_inbound)):echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound',
$vendor)\">$<script>if ( $( this ).hasClass( 'half' ) )
{document.write(inboundAmount/2);}</script></a>"; endif; ?></td>
And I have also tried the manipulation in the success area of my ajax script.
Answering some of the questions below:
$inbound_status first$id is a PHP variable.
The output of document.write(inboundAmount/2); is working correctly and showing half the amount of document.write(inboundAmount);
The this scope inside your <script> tag will not point to the element you expect. Instead of checking in javascript you can check the condition in php itself. Otherwise, you can refer the code below to do it on javascript. You can use the php variable to check whether it contains the string half
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">
$<script>if ('$inbound_status' == 'half' )
{document.write(inboundAmount/2);}
</script>
</a>";
endif;
echo "</td>";
If inboundAmount is a php variable you can do it easily without using javascript
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">$";
if ($inbound_status == 'half'):
echo $inboundAmount/2;
endif;
echo "</a>";
endif;
echo "</td>"
I have many links which are dynamically created with PHP and now I want to add an onclick event.
while($row = mysqli_fetch_array($xyz)) {
echo "<tr>";
echo "<td><a href=\"test.php?parameter=". $row['z'] ."\" target=\_blank>" . $row['z'] . "</a></td>";
echo "</tr>";}
but I want like this:
while($row = mysqli_fetch_array($xyz) {
echo "<tr>";
echo "<td><a href=\"test.php?parameter=". $row['z'] ."\" target=\_blank>" . $row['z'] . onclick="test()"</a></td>";
echo "</tr>";}
is this possible?
I am not sure if I understood what exactly you are asking but I try to answer you the same:
You can't record a normal JS variable in a page session and then to access to it after the loading of a page.
For this case you can use cookies (that are accessible via server-side too):
// put this code into methods that users suggested you.
document.cookie = "value_selected=something"
and once page is loaded:
var cookies = document.cookie.split(';');
var value_selected = JSON.parse(JSON.stringify(cookies[YOUR_COOKIE].split("=")[0]))
or server side:
echo "<td><a href=\"test.php?parameter=". $row['z'] ."\" data-something=htmlentities($_COOKIE['value_selected']) target=\_blank>" . $row['z'] . "</a></td>"; //(example)
Or more simply, pass the value selected through query
?value_selected=something //elaborate the new link in the method users suggested you
and
in server-side, use $['GET'] to obtain all values in query string.
IMHO I think the first choice is simpler than second.
I hope I helped you
Try Like this
while($row = mysqli_fetch_array($xyz) {
echo "<tr>";
echo "<td><a class='myclick' href=\"test.php?parameter=". $row['z'] ."\" target=\_blank>" . $row['z'] . **onclick="test()"**</a></td>";
echo "</tr>";}
i have added a class to link as myclick
and in jquery
$(document).on('click','.myclick',function(e){
e.preventDefault();
alert('clicked');
});
You can try like this..just a simple way.
<script type="text/javascript">
function test(){
alert('test');
}
</script>
<?php
while($row = mysqli_fetch_array($xyz) {
echo "<tr>";
echo "<td><a href='your link' onclick='test()'></a></td>";
echo "</tr>";
}
?>
I'm trying to add a script element to the HTML from the PHP code and instantly remove it so it won't be visible in the HTML. The script only contains things to execute at the same moment and not functions. Generally, I'm trying to replicate ASP.NETs runat property, so I'll be able to set values of elements (inputs for now) right from the PHP code.
This is what I tried so far (which I found in a different question, with some changes of mine) and it adds the script properly, but won't remove it.
function JSSetValue($id, $value) // Input 'value' only
{
echo '<script>
var input = document.getElementById("' . $id . '");
input.value = "' . $value . '"
</script>';
$html = <<<HTML
...
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$script = $dom->getElementsByTagName('script');
foreach($script as $item)
{
$item->parentNode->removeChild($item);
break;
}
$html = $dom->saveHTML();
}
Thanks for everyone who were trying to help. Anyway, I found a solution to my question, which is this:
function JSSetValue($id, $value) // Input 'value' only
{
echo '<script id="phpjs">
var input = document.getElementById("' . $id . '");
input.value = "' . $value . '";
document.getElementById("phpjs").remove();
</script>';
}
It adds the script and removes it, so it won't be visible when inspecting elements anymore.
I am trying to use jQuery to pass some values to session variables when buttons are clicked on. Each button has unique values and I need to display values from both buttons when both are clicked.
The problem I am having is that only one set of values (from the most recent button clicked) is translated as a session variable - if I click the first button, the first values are passed to the session backend fine, but clicking the second button afterwards results in only the second values being passed.
My jquery looks like this:
$button_one.click(function(e) {
e.preventDefault();
var var_one = "value1";
var var_two = "value2";
$.post("sessions.php", { 'session_var_one': var_one, 'session_var_two': var_two });
});
$button_two.click(function(e) {
e.preventDefault();
var var_three = "value3";
var var_four = "value4";
$.post("sessions.php", { 'session_var_three': var_three, 'session_var_four': var_four });
});
The sessions.php is very simple:
<?php
session_start();
$_SESSION['value_one'] = $_POST['session_var_one'];
$_SESSION['value_two'] = $_POST['session_var_two'];
$_SESSION['value_three'] = $_POST['session_var_three'];
$_SESSION['value_four'] = $_POST['session_var_four'];
?>
And I have a simple page set up to display the session values:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo '<h5>' . $_SESSION['session_var_one'] . '</h5>';
echo '<h5>' . $_SESSION['session_var_two'] . '</h5>';
echo '<h5>' . $_SESSION['session_var_three'] . '</h5>';
echo '<h5>' . $_SESSION['session_var_four'] . '</h5>';
?>
</body>
</html>
This page displays only the two values of the button that was last clicked on - instead of displaying both sets of values if both buttons are clicked on.
I am guessing that having two separate AJAX requests, one within each click function, may be the problem here - when button two is clicked after button one, it "forgets" the first request and thus the values are not recorded.
I have worked around this problem by sending each of the button click values to a different session php page (i.e. button_one goes to sessions_one.php and button_two goes to sessions_two.php) but I would prefer not to have to create a new place to store the session values for each button. I plan on adding more buttons and it seems like bad practice to have each button have a separate home for its stored values.
How can I rewrite my AJAX requests and/or sessions.php so that I can store all of the values from each button click? Thank you for any guidance, I'm very new to PHP and AJAX in general!
If a $_POST variable is not provided, your code will just put NULL in the corresponding $_SESSION variable. To prevent that, check that it is provided before doing so:
<?php
session_start();
if( isset($_POST['session_var_one']) ){
$_SESSION['value_one'] = $_POST['session_var_one'];
}
if( isset($_POST['session_var_two']) ){
$_SESSION['value_two'] = $_POST['session_var_two'];
}
if( isset($_POST['session_var_three']) ){
$_SESSION['value_three'] = $_POST['session_var_three'];
}
if( isset($_POST['session_var_four']) ){
$_SESSION['value_four'] = $_POST['session_var_four'];
}
?>
I think you really want something like this so the session data stays if you aren't passing it a new value.
$_SESSION['value_one'] = (!empty($_POST['session_var_one'])) ? $_POST['session_var_one'] : $_SESSION['value_one'];
$_SESSION['value_two'] = (!empty($_POST['session_var_two'])) ? $_POST['session_var_two'] : $_SESSION['value_two'];
$_SESSION['value_three'] = (!empty($_POST['session_var_three'])) ? $_POST['session_var_three'] : $_SESSION['value_three'];
$_SESSION['value_four'] = (!empty($_POST['session_var_four'])) ? $_POST['session_var_four'] : $_SESSION['value_four'];
To display you should to call to session not post and it will be like this:
<?php
echo '<h5>' . $_SESSION['value_one'] . '</h5>';
echo '<h5>' . $_SESSION['value_two'] . '</h5>';
echo '<h5>' . $_SESSION['value_three'] . '</h5>';
echo '<h5>' . $_SESSION['value_four'] . '</h5>';
?>
I have 2 php pages, htmldisp.php and classfn.php. And an alert used in hrmldisp.php is not working. It should actually give a dropdown value.
htmldisp.php is
<?php include("classfn.php"); $obj=new hello;?>
<script language="javascript">
function submitted(){
var select=document.getElementById('newdrop').value;
alert(select);
}
</script>
<?php
$id="newdrop";
echo $obj->hellofn($id); ?>
<input type="submit" onclick="submitted();"
classfn.php is
<?php
class hello{
function hellofn($id){
$s="select `Name`,`Value` from `Days`;
$q=mysql_query($s);
$p='<td>'.'<select id="$id">';
while($re=mysql_fetch_array($q)){
$value=$re["Value"];
$name=$re["Name"];
$p.='<option value="' . $value . '"' . ( $selected==$value ? ' selected="selected"' : '' ) . '>' . $name . '</option>';
}
$p.='</select>'.'</td>';
return $p;
}
?>
The problem is alert(select) not working. Thanks
As far as I can tell you never call the function submitted. On top of that your HTML is completely malformed. You have a bunch of html and javascript outside of your html tag. And you have 2 opening html tags with no close...