PHP - Easy Question: Echo And Return?
Hey people!
I've been setting a coding script for experience and this is my situation: To buy a virtual flower, one needs to have 50 virtual coins. So I first extract the number of coins the person has from the DB. If the user doesn't have the sufficient number of coins, the BUY button will not be available and vice versa. When the BUY button is clicked, I update the database, adding the flower to the user's inventory and subtracting 50 from the total number of coins the user has. This I do in that page itself using if(isset($_POST['submit'])) Now, I need to know how I could check whether the user has at least 50 coins even if the button is available. I hope that you could understand this. I want to do it for validation purposes. Its to prevent the user spending the coins he has in another browser window and then returning to the visible button to click it. I hope that I make sense. Thanks in advance! Regards, Thauwa Similar TutorialsI have this working script below. I never managed to turn it into a function... I think it's because there's classes in it.
I stopped that investigation and instead I choose to:- keep it on another page, trigger it with the a <form> button, then return to the form page using header location. This is probably much more elaborate than it needs to be... but it seems to be working fine.
I'd like to print out "email sent" if successful. The collection of confusion at the end of this script, is me trying to get it to return the - "email sent" message <index.php> <?php require 'includes/PHPMailer.php'; require 'includes/SMTP.php'; require 'includes/Exception.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; $mail = new PHPMailer(); $mail->isSMTP(); $mail->Host = "smtp.gmail.com"; $mail->SMTPAuth = "true"; $mail->SMTPSecure = "tls"; $mail->Port = "587"; $mail->Username = "email@gmail.com"; $mail->Password = "pass"; $mail->Subject = "Test email using PhPmailer"; $mail->setFrom("email@gmail.com"); $mail->isHTML(true); //$mail->addAttachment('img/movie.mp4'); $mail->Body = "<h1>This is a HTML heading</h1></br><p>This is a html title</p>"; $mail->addAddress("email@gmail.com"); if ($mail->Send() ) { echo "Email sent..!"; } else{ echo "Error..!"; } $mail->smtpClose(); ob_start(); $Message = urlencode("Email Sent"); header("Location:EmailBut.html?Message=".$Message); die; ob_flush(); ?> The stuff at the beginning of this next script is me trying to get it to print out email sent. I can get it to return the message in the URL but it won't print out. <EmailBut.html> <?php ob_start(); if(isset($_GET['Message'])){ echo $_GET['Message']; } echo $_GET['Message']; ob_flush(); ?> <html> <body> <form action="index.php" method="POST"> <button type="submit">Send</button> </form> </body> </html> Can you please give me a push in the right direct here? Edited January 30, 2020 by JonnyDrillerhi guys i am including a function in my main script, and i'm calling it aftwerwards. except i cannot echo successfully the data in the $result variable, why? function echoResult(){ $result = 'dan'; return $result; } // 1 echo echoResult(); // don't work. i get empty data. // 2 $fnResult = echoResult(); echo $fnResult; // don't work. get empty data. i have ran get_included_files and the include is being made successfully. i read on php.net and these are the examples they use (user comments!) how do i get the data out of the function and echo it, using this way? I want to know can we do text formatting to a result coming from echo if so then kindly guide how? I need make error lower then 3 and higher then 15 how should i add higher then 15? Code: [Select] if (strlen($searchTerms) < 3) { $error[] = "Add more then 3 symbols and less then 15!"; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } Right now I'm working on a simple register form and I have included the theme. I set up a div and positioned the div below the register form to output any error in red text. If I were to use code like this if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email']) || empty($_POST['Veremail'])){ $required_info = "All three fields are required to continue!"; } It will not work, because I don't have a die(); or end(); function, so it will display: " All three fields are required to continue! and You have been successfully registered! " BUT IT'S IN THE CORRECT PLACE UNDER THE FORM WHERE I PLACED THE DIV. So I made this change if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email']) || empty($_POST['Veremail'])){ $required_info = die("All three fields are required to continue!"); } and it worked fine, but now, the words are not positioned under the form, but at the bottom of the page, I'm wondering how I would end that script, but align the text so it displays underneath the form, and not at the bottom of the page? If you don't understand ask me please. While going through an instructional book, I ran across some code that I didn't immediately understand. I've stripped it down to the relevant bits: function example($object_a, $object_b) { $compare = $object_a->id == $object_b->id; return another_function('argument 1') && $compare || another_function('argument 2'); } My first confusion was this line: $compare = $object_a->id == $object_b->id; Does this set $compare to TRUE if the ids match and FALSE if they do not? And the second confusion: return another_function('argument 1') && $compare || another_function('argument 2'); What is returned if the ids match? What is returned if they are different? When I try testing this out on my machine, it returns both functions whether or not the ids match, but I know that's not what's supposed to happen. Can anyone break this down for me? Thanks. So I am extending the DateTime class and would like to completely disable the modify() function that is built in so i have this: Code: [Select] <?php class NewDateTime extends DateTime { public function modify() { throw new Exception('modify() has been disabled.'); } } thats not the entire class description, just the pertinent part of course now any time I create a new object from NewDateTime I get this message at the top of the screen: Quote Strict Standards: Declaration of NewDateTime::modify() should be compatible with that of DateTime::modify() in C:\xampp\htdocs\.....my file path here.....\NewDateTime.php on line 62 what gives? how do I get rid of this and still disable modify()? I'm betting its a setting that i have wonky but I am really rusty in php so dont know where to look in php.ini hello. i want to know what is *$ or +$ because i see it in many contact forms. also, what is the difference between these: /^[A-Z]+[a-z]*$/ /^[A-Z]+[a-z]+$/ /^[A-Z]+[a-z]$/ /^[A-Z]+[a-z]/ also, which is the shortest and efficient method of checking somebodys name to be exactly "Name Surname" - "Green Trancer" ? in my form, i use var filter = /^[A-Z]+[a-z]*$/; var filter2 = /^[A-Z]+[a-z]+\s+[A-Z]+[a-z]+$/; and then i check the name to be like filter or filter2. how can i check that between the words to be exactly one space character ? thank you very much. I'm trying to search the 'ad' column where 'ad' = 1 or 3 I'm having problems doing this. Here is what I currently have, but it is not bringing back 'ad' where ='1' (for obvious reasons) Quote $query = "SELECT * FROM users WHERE ad='3' AND state='$state1'"; Here is what I thought would work, but it displays every listing in my database when I do it this way. Quote $query = "SELECT * FROM users WHERE ad='3' OR ad='1' AND state='$state1'"; Thank you in advance for your answers, google is not being so kind to me right now. Hello Everybody
I usually work with C#, but I'm doing a website right now and have a question:
I want a picture in my header and when I change the window size the image may not move.
My code so far:
#a1 { position: relative; top:-175px; left:0px; width:50px; height:50px; background-image:url(http://domain.png); background-repeat:no-repeat;} </style>Hope someone can answer this question easy I have a form that I'm posting the contents to a 3rd party data management server via cURL. They return XML to let you know if the data was posted successfully or not. I'm trying to figure out how I can take that return XML and parse the data out of there. Quote <?php extract($_POST); $url = 'https://www.*domain*.com/DataReceiver'; $fields = array( blah blah blah ); foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string,'&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); $result = curl_exec($ch); Now my question is, when the data posts and the XML is returned, how do I capture that? When I don't use cURL and post directly using HTTP POST, I get the XML returned at the exact same address I post to. So to process the XML returned, is this code correct? Quote $xml_feed_url = 'https://www.*domain*.com/DataReceiver'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $xml_feed_url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $xml = curl_exec($ch); curl_close($ch); I'm kind of at a loss on what to do from this point... Thanks to anyone who takes the time to read and can help! Wow, am I glad I found this forum! I spent the past few hours trying to modify a PHP/mySQL script in order to test it. What this script does is basically look up a mySQL database (based on ip2nation) to find out what country and currency the IP address belongs to. This is the code sample: Code: [Select] $sql = 'SELECT c.country, c.curr_code FROM ip2nationcountries c, ip2nation i WHERE i.ip < INET_ATON("24.24.24.24") AND c.code = i.country ORDER BY i.ip DESC LIMIT 0,1'; $countryName = $db->get_sql_row($sql); I tested the code in PHPmyAdmin and it returns 2 rows: country ------- curr_code (row names) United States ------- USD (actual values) So basically 2 variables, United States and USD. Now comes the problem, in order to test the script for several countries, I need to be able to "override" the result in the PHP script I tried to do that by commenting out the part that returns the result from the mySQL query and modifying it with the following style, but I think I'm doing something wrong: Code: [Select] ### $countryName = $db->get_sql_row($sql); ### $countryName = "United States USD"; Is there any way that I can temporarily override the result returned by mySQL and "fake it" so to say? I understand the mySQL query returns 2 rows, so how would I go about overriding 2 rows? I hope this makes sense ...you're all my last hope. How can I use an include file that has a list of links in any directory at my website without breaking those links? You can see it in action here... The links in this directory work... https://www.billelgin.com/primary-directory/ The links in this directory (being created from the same include file) do not work... https://www.billelgin.com/primary-directory/subsection-one/ Obviously the problem is the include file is being called from different directories, so it's breaking the relative path links in the include file, but what can I do to fix it? You can download the example directory structure here... https://www.billelgin.com/EXAMPLE.zip Due to my home brewed templating system I must use 'return' instead of 'echo' or 'print'. I have a simple database query and running a while loop where all rows are to "printed" to the browser. But when I use return only the first row is shown. Using echo will print out all of the rows as it should but of course it prints the rows outside of where they should in the template. This may be very simple but I am just starting out. I tried to use the forum's search to see if this has been answered but the daemon could not 'be reached' and searching on google brings up a ton of non-helpful links. The solution will be very helpful as I will be doing this A LOT in my scripts for my current project. Thank you! Alright, so I just started picking up PHP again and I have a pretty simple question. I have a html form that takes a string then writes it on another page. My problem is when I use quotations I'll get slashes in them on the page it writes the string on. Is there a way to clean that stuff up? I am very new to php and am trying to create a simple application that uploads a PDF file to a database. I have one field for the Volume Number and on file field for the PDF to be uploaded. My issue is i can't get the PDF to upload or insert the name of the pdf (eg volume1.pdf) into the data base. I would also like to point out that I know i have a low post count, but i only seek help when i truly need it and have exhausted all other resources... Here is what i have, please go easy on me this is my first round at php: Code: [Select] <?php if(isset($_POST['submit'])){ $vol_num = $_POST['vol_num']; $pdf = $_FILES['pdf']['name']; $path = '../pdf/'.$_FILES['pdf']['name']; move_uploaded_file($_FILES["pdf"]["tmp_name"], $path); mysql_query("INSERT INTO volumes set vol_num='$vol_num', vol_link='$pdf'") or die (mysql_error()); echo "<script>location.href='add_volume.php'</script>"; } ?> what am i doing wrong here? Thanks in advance Hello!Can i echo data from 2 or more tables on the same page? Hey guys,
I am using a function that echos out 10 images from my database. I need to have a minimum of 4 echoed images. Some categories have less than 4 images, so I would like to have a default image(s) echoed out if the database has 4 or less images.
Does anyone know how I can do this?
Below is the code I am using now to echo out the 10 images.
Thanks in advance:
<?php $query = mysql_query('SELECT image_id FROM images ORDER BY RAND() LIMIT 10'); $i = 0; class myCounter3 implements Countable { public function count() { static $count = 0; return ++$count; } } $counter = new myCounter3; while ($row = mysql_fetch_array($query)) { if($i % 10 === 0) { } echo '<img src="http://www.mysite.com/'.$row['image_id'].'.jpg" width="300" height="auto"/>'; $i++; } ?> Default Image: <img src="http://www.mysite.com/default.jpg" width="300" height="auto"/> I set up a form that you can type html code into. When you click submit it sends the form data to a php script that receives the data and then prints it. Basically im trying to accomplish something similar to this http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro. I have tried to print the html code using echo and print_r. The problem is that it is not displaying correctly. For instance if i were to type in Code: [Select] <body bgcolor="FFCC00"> Nothing would happen. Most of the html displays correctly. What is the reason behind this? Do i need to use a different command to print the form variable? Or do i need to save it as an html document then display it? OK, have no idea what's going on... I've done this a million times... why wont this output!?? I must have a major brain meltdown and dont know it yet!!! Code: [Select] <?php // this echoes just fine: echo $_POST['testfield']; // but this wont echo: echo if (isset($_POST['testfield'])) { $_POST['testfield'] = $test; } echo $test; /// or even this DOESNT echo either!: $_POST['testfield'] = $test; echo $test; ?> |